---
title: Simple random world using interaction
slug: world-sdk-guide/simple-random-world-using-interaction
docTags: 
createdAt: 2024-09-02T08:34:13.138Z
---

## Sample Project

:::hint{type="info"}
📘 **Project files used in lecture**

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

## Summary

| Summary       | Implement the interaction buttons that appear when the user approaches you. |
| ------------- | --------------------------------------------------------------------------- |
| Difficulty    | Beginner                                                                    |
| Time Required | 30 Minutes                                                                  |



**Project Goals**

- Interactive button controls applying UI and trigger
- World Space Canvas
- Apply Character Animation
- Creating an Object Instance
- Apply Probability Random Code



**🎬 Video of Completed World**

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



## Scripts

:::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 custom script from path
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 character
        ZepetoPlayers.instance.OnAddedLocalPlayer.AddListener(() => {
            this.zepetoCharacter = ZepetoPlayers.instance.LocalPlayer.zepetoPlayer.character;
        });
        // Script import
        this.uiController = this.uiControllerObject.GetComponent<UIController>();
        
        //Button Hide
        this.openUIGesture.gameObject.SetActive(false);
        
        //When Button Click
        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 Create
        var obj = Object.Instantiate(this.badEffectFactory) as GameObject;
        obj.transform.position = this.transform.position;
    }

    private Win()
    {
        this.uiController.Win();
        //GameObject Create
        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();
        //Destroy box
        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 = "What is in the box?";
    }

    public Win()
    {
        this.messageUI.text = "Congrats! You got it";
    }

    public Lose()
    {
        this.messageUI.text = "It's a blank";
    }

}
```
:::

