TUTORIALS
물체와 인터렉션하는 월드를 만들어 봅니다.
6min
샘플 프로젝트 📘 강의에 사용된 프로젝트 파일 https //github com/naverz/zepeto world sample/tree/main/assets/chapter3 https //github com/naverz/zepeto world sample/tree/main/assets/chapter3 요약 요약 사용자가 가까이에 다가갔을 때 나타나는 상호작용 버튼을 구현해 봅니다 난이도 초급 소요 시간 30분 프로젝트 목표 ui와 트리거를 응용한 상호작용 버튼 컨트롤 월드 스페이스 캔버스 캐릭터 애니메이션 적용 오브젝트 인스턴스 생성 확률 랜덤 코드 적용 🎬 완성 월드 영상 https //www youtube com/watch?v=ooazdb4 lgo 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() { //사용자 id를 가져옵니다 (편집기를 통해 로그인됨) 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(); // 게임 오브젝트 생성 var obj = object instantiate(this badeffectfactory) as gameobject; obj transform position = this transform position; } private win() { this uicontroller win(); // 게임 오브젝트 생성 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'; //변수를 정의합니다 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 = "빈 공간입니다"; } }