---
title: โลกแบบสุ่มง่ายๆ โดยใช้การโต้ตอบ
slug: world-sdk-guide-th/oHiHNjyQ_XJ_K-YEaXPAu
docTags: 
createdAt: 2024-09-02T08:34:13.138Z
---

## ตัวอย่างโครงการ

:::hint{type="info"}
📘 **ไฟล์โครงการที่ใช้ในบรรยาย**

- [https://github.com/naverz/zepeto-world-sample/tree/main/Assets/Chapter3](https://github.com/naverz/zepeto-world-sample/tree/main/Assets/Chapter3)
:::

## สรุป

| สรุป           | ดำเนินการปุ่มโต้ตอบที่ปรากฏเมื่อผู้ใช้เข้ามาใกล้คุณ |
| -------------- | --------------------------------------------------- |
| ความยาก        | ระดับเริ่มต้น                                       |
| เวลาที่ต้องใช้ | 30 นาที                                             |



**เป้าหมายของโครงการ**

- ปุ่มควบคุมแบบโต้ตอบที่ใช้ UI และทริกเกอร์
- ผืนผ้าใบในโลก
- ใช้การเคลื่อนไหวของตัวละคร
- สร้างอินสแตนซ์ของวัตถุ
- ใช้รหัสสุ่มความน่าจะเป็น



**🎬 วิดีโอของโลกที่เสร็จสมบูรณ์**

::embed[[https://www.youtube.com/watch?v=OoAzdB4_lgo](https://www.youtube.com/watch?v=OoAzdB4_lgo)]{url="https://www.youtube.com/watch?v=OoAzdB4_lgo"}



## สคริปต์

:::CodeblockTabs
CharacterController.ts

```typescript
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;
        });
    }
}               
```
:::

:::CodeblockTabs
Interaction.ts

```typescript
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);
    }
}
```
:::

:::CodeblockTabs
UIController.ts

```typescript
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 = "มันเป็นช่องว่าง";
    }

}
```
:::

