บทเรียน
โลกแบบสุ่มง่ายๆ โดยใช้การโต้ตอบ
6 นาที
ตัวอย่างโครงการ 📘 ไฟล์โครงการที่ใช้ในบรรยาย https //github com/naverz/zepeto world sample/tree/main/assets/chapter3 สรุป true left unhandled content type left unhandled content type left unhandled content type left unhandled content type left unhandled content type left unhandled content type เป้าหมายของโครงการ ปุ่มควบคุมแบบโต้ตอบที่ใช้ ui และทริกเกอร์ ผืนผ้าใบในโลก ใช้การเคลื่อนไหวของตัวละคร สร้างอินสแตนซ์ของวัตถุ ใช้รหัสสุ่มความน่าจะเป็น 🎬 วิดีโอของโลกที่เสร็จสมบูรณ์ https //www youtube com/watch?v=ooazdb4 lgo สคริปต์ charactercontroller ts import { zepetoscriptbehaviour } from 'zepeto script' import { spawninfo, zepetoplayers, localplayer, zepetocharacter } from 'zepeto character controller' import { worldservice } from 'zepeto world'; export default class charactercontroller extends zepetoscriptbehaviour { start() { //gets the userid (logged in through editor) zepetoplayers instance createplayerwithuserid(worldservice userid,new spawninfo(), true); zepetoplayers instance onaddedlocalplayer addlistener(() => { let player localplayer = zepetoplayers instance localplayer; }); } } interaction ts import { canvas, animationclip, waitforseconds, gameobject, object, random } from 'unityengine'; import { button } from 'unityengine ui'; import { zepetoplayers, zepetocharacter } from 'zepeto character controller'; import { zepetoscriptbehaviour } from 'zepeto script'; // นำเข้าสคริปต์ที่กำหนดเองจากเส้นทาง import uicontroller from ' /uicontroller'; export default class interaction extends zepetoscriptbehaviour { public openuigesture button; public interactioncanvas canvas; public animationclip animationclip; public uicontrollerobject gameobject; public badeffectfactory gameobject; public goodeffectfactory gameobject; public gift gameobject; public failureratio number = 50; private uicontroller uicontroller; private zepetocharacter \ zepetocharacter; start() { // ตั้งค่า eventcamera this interactioncanvas worldcamera = zepetoplayers instance zepetocamera camera; // ตั้งค่าตัวละคร zepetoplayers instance onaddedlocalplayer addlistener(() => { this zepetocharacter = zepetoplayers instance localplayer zepetoplayer character; }); // นำเข้าสคริปต์ this uicontroller = this uicontrollerobject getcomponent\<uicontroller>(); //ซ่อนปุ่ม this openuigesture gameobject setactive(false); //เมื่อคลิกปุ่ม this openuigesture onclick addlistener(()=>{ this zepetocharacter setgesture(this animationclip); this startcoroutine(this firstroutine()); }); } ontriggerenter(collider) { this openuigesture gameobject setactive(true); } ontriggerexit(collider) { this openuigesture gameobject setactive(false); } firstroutine() { this uicontroller loading(); yield new waitforseconds(3); this zepetocharacter cancelgesture(); this randomcalculation(); } private randomcalculation() { let randomnumber number; randomnumber = random range(0,100); if (randomnumber <= this failureratio) { this lose(); } else { this win(); } this startcoroutine(this secondroutine()); } private lose() { this uicontroller lose(); //สร้าง gameobject var obj = object instantiate(this badeffectfactory) as gameobject; obj transform position = this transform position; } private win() { this uicontroller win(); //สร้าง gameobject var obj = object instantiate(this goodeffectfactory) as gameobject; obj transform position = this transform position; var giftobj = object instantiate(this gift) as gameobject; giftobj transform position = this transform position; } secondroutine() { yield new waitforseconds(1); this uicontroller init(); //ทำลายกล่อง gameobject destroy(this gameobject); } } uicontroller ts import { zepetoscriptbehaviour } from 'zepeto script'; import { text } from "unityengine ui"; import { gameobject } from 'unityengine'; //define variable to use export default class uicontroller extends zepetoscriptbehaviour { public messageui text; start() { this init(); } public init() { this messageui text = " "; } public loading() { this messageui text = "กล่องนี้มีอะไรอยู่?"; } public win() { this messageui text = "ยินดีด้วย! คุณทำได้แล้ว"; } public lose() { this messageui text = "มันเป็นช่องว่าง"; } }
