สร้างโลกของคุณเอง
ผู้เล่นและตัวละคร: เคล็ดลับ
การวัดความสูงของตัวละครของคุณ
3นาที
ในคู่มือนี้ เราจะแสดงให้คุณเห็นวิธีการวัดความสูงของตัวละคร zepeto ของผู้เล่นท้องถิ่นตามประเภทของร่างกาย ขั้นตอนที่ 1 เขียนสคริปต์เพื่อดึงค่าความสูงของตัวละคร เพิ่ม gameobject ลงในฉากและเพิ่มสคริปต์ตัวอย่าง characterheight ด้านล่างนี้ import { debug, humanbodybones, vector3, waitforendofframe } from 'unityengine'; import { localplayer, zepetoplayers, zepetoplayer, knowsockets } from 'zepeto character controller'; import { zepetoscriptbehaviour } from 'zepeto script'; export default class characterheight extends zepetoscriptbehaviour { start() { zepetoplayers instance onaddedlocalplayer addlistener(() => { let player localplayer = zepetoplayers instance localplayer; this startcoroutine(this cogetzepetoheight( player zepetoplayer)); }); } cogetzepetoheight(zepeto zepetoplayer) { // ใช้ waitforendofframe เพื่อให้ได้ตำแหน่งของข้อต่อของตัวละครอย่างแม่นยำ yield new waitforendofframe(); // รับตำแหน่งของหัว const headposition = zepeto character getsocket(knowsockets head upper) position; // รับตำแหน่งของเท้าซ้าย const leftfootposition = zepeto character zepetoanimator getbonetransform(humanbodybones leftfoot) position; // รับตำแหน่งของเท้าขวา const rightfootposition = zepeto character zepetoanimator getbonetransform(humanbodybones rightfoot) position; // คำนวณจุดกึ่งกลางระหว่างเท้าทั้งสอง const charactercenter = vector3 lerp(leftfootposition, rightfootposition, 0 5); // คำนวณระยะห่างระหว่างหัวและจุดกึ่งกลางของตัวละคร const characterheight = vector3 distance(headposition, charactercenter); console log(`ความสูงของตัวละครจากพื้น ${characterheight}`); } } คำอธิบายสคริปต์ cogetzepetoheight(zepeto zepetoplayer) ใช้ getsocket เพื่อรับค่าตำแหน่งของข้อต่อหัวของตัวละคร เพื่อรับค่าตำแหน่งของจุดกึ่งกลางด้านล่างของตัวละคร ให้ใช้ getbonetransform เพื่อรับค่าตำแหน่งของเท้าทั้งสองของตัวละคร ใช้ vector3 lerp เพื่อรับค่าตำแหน่งกึ่งกลางด้านล่างของตัวละคร รับค่าความสูงของตัวละครโดยใช้ค่าตำแหน่งของข้อต่อหัวของตัวละครและค่าตำแหน่งกึ่งกลางด้านล่าง ขั้นตอนที่ 2 รัน เมื่อคุณรันโดยการคลิกปุ่มเล่น คุณจะเห็นค่าความสูงของตัวละครในคอนโซลล็อก 👍 เคล็ดลับ วิธีการวัดความสูงในตัวอย่างคือการวัดความสูงของตัวละครโดยใช้ค่าความสูงของ headsocket หากคุณต้องการวัดตำแหน่งด้านบนโดยการสร้างโมเดลหัวของตัวละคร หมวก และอุปกรณ์เสริม โปรดดูที่ลิงก์ต่อไปนี้ 📘 skinnedmeshrenderer localbounds https //docs unity3d com/manual/class skinnedmeshrenderer html https //docs unity3d com/manual/class skinnedmeshrenderer html https //docs unity3d com/2020 3/documentation/scriptreference/skinnedmeshrenderer localbounds html https //docs unity3d com/2020 3/documentation/scriptreference/skinnedmeshrenderer localbounds html