チュートリアル
相互作用を使用したシンプルなランダムワールド
6分
サンプルプロジェクト 📘 講義で使用されるプロジェクトファイル https //github com/naverz/zepeto world sample/tree/main/assets/chapter3 https //github com/naverz/zepeto world sample/tree/main/assets/chapter3 概要 概要 ユーザーが近づくと表示されるインタラクションボタンを実装します。 難易度 初心者 必要な時間 30分 プロジェクトの目標 インタラクティブボタンコントロールの適用とトリガー ワールドスペースキャンバス キャラクターアニメーションの適用 オブジェクトインスタンスの作成 確率ランダムコードの適用 🎬 完成した世界のビデオ 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(); //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'; //使用する変数を定義 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 = "空白です"; } }