---
title: Dunia acak sederhana menggunakan interaksi
slug: world-sdk-guide-id/dunia-acak-sederhana-menggunakan-interaksi
docTags: 
createdAt: 2024-09-02T08:34:13.138Z
---

## Proyek Contoh

:::hint{type="info"}
📘 **Berkas proyek yang digunakan dalam kuliah**

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

## Ringkasan

| Ringkasan             | Implementasikan tombol interaksi yang muncul ketika pengguna mendekati Anda. |
| --------------------- | ---------------------------------------------------------------------------- |
| Kesulitan             | Pemula                                                                       |
| Waktu yang Diperlukan | 30 Menit                                                                     |



**Tujuan Proyek**

- Kontrol tombol interaktif menerapkan UI dan pemicu
- Kanvas Ruang Dunia
- Terapkan Animasi Karakter
- Membuat Instansi Objek
- Terapkan Kode Acak Probabilitas



**🎬 Video Dunia yang Selesai**

::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"}



## Skrip

:::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() {
        //Mengambil UserId (masuk melalui 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 skrip kustom dari jalur
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() 
    {    
        // Set EventCamera 
        this.interactionCanvas.worldCamera = ZepetoPlayers.instance.ZepetoCamera.camera;
        // Set karakter
        ZepetoPlayers.instance.OnAddedLocalPlayer.AddListener(() => {
            this.zepetoCharacter = ZepetoPlayers.instance.LocalPlayer.zepetoPlayer.character;
        });
        // Impor skrip
        this.uiController = this.uiControllerObject.GetComponent<UIController>();
        
        //Tombol Sembunyikan
        this.openUIGesture.gameObject.SetActive(false);
        
        //Ketika Tombol Diklik
        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();
        //Buat GameObject
        var obj = Object.Instantiate(this.badEffectFactory) as GameObject;
        obj.transform.position = this.transform.position;
    }

    private Win()
    {
        this.uiController.Win();
        //Buat 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();
        //Hancurkan kotak
        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 = "Apa yang ada di dalam kotak?";
    }

    public Win()
    {
        this.messageUI.text = "Selamat! Anda mendapatkannya";
    }

    public Lose()
    {
        this.messageUI.text = "Ini kosong";
    }

}
```
:::

