Studio GuideWorld SDK Guide
Log In
World SDK Guide

Make an item eating World

Sample Project

📘

Project files used in lecture


Summary

SummaryBased on what we learned in Chapter 1, we will make an item eating world using character movement speed and jump control.
DifficultyBeginner
Time Required30 Minutes

Project Goals

  • Handling of item acquisition using OnTrigger event
  • Understand how to apply Text UI
  • Create UI Controller to reflect item acquisition in Text UI
  • Understand how to control the height of the ZEPETO character's jump
  • Understand how to control the movement speed of ZEPETO characters

🎬 Video of Completed World


Scripts

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;
        });
    }
}               
import { Collider, Vector3, GameObject, Transform } from 'UnityEngine';
import { ZepetoCharacter, ZepetoPlayer, ZepetoPlayers } from 'ZEPETO.Character.Controller';
import { ZepetoScriptBehaviour, ZepetoScriptInstance } from 'ZEPETO.Script';
 
// import custom script from path
import UIController from './UIController';
 
export default class GetItem extends ZepetoScriptBehaviour 
{
   public extComponentGameObject: GameObject;
   private extComponent: UIController;
   private zepetoCharacter: ZepetoCharacter;

    Start() 
    {
        //Zepeto character object
        ZepetoPlayers.instance.OnAddedLocalPlayer.AddListener(() => {
            this.zepetoCharacter = ZepetoPlayers.instance.LocalPlayer.zepetoPlayer.character;
        });
        //script import
        this.extComponent = this.extComponentGameObject.GetComponent<UIController>();
    }


    OnTriggerEnter(collider: Collider) 
    {
        if (this.zepetoCharacter == null || collider.gameObject != this.zepetoCharacter.gameObject)
            return;
        
        //Sends a message of raising a count after eating an item
        this.extComponent.IncreaseCount();
        //Destroy item
        GameObject.Destroy(this.gameObject);
   }
}
import { Collider } from 'UnityEngine';
import { ZepetoPlayers, ZepetoCharacter } from 'ZEPETO.Character.Controller';
import { ZepetoScriptBehaviour } from 'ZEPETO.Script';

export default class JumpPower extends ZepetoScriptBehaviour 
{
    public jumpPower: number = 5;
    private zepetoCharacter :ZepetoCharacter;

    Start() 
    {
        //Zepeto character object
        ZepetoPlayers.instance.OnAddedLocalPlayer.AddListener(() => {
            this.zepetoCharacter = ZepetoPlayers.instance.LocalPlayer.zepetoPlayer.character;
        });      
    }

    OnTriggerEnter(collider: Collider) 
    {
        if (this.zepetoCharacter == null || collider.gameObject != this.zepetoCharacter.gameObject)
            return;
        
        //Add Jumppower
        this.zepetoCharacter.additionalJumpPower = this.jumpPower;
    }

    OnTriggerExit(collider: Collider) {
        //Turn the jump power back to normal
        this.zepetoCharacter.additionalJumpPower = 0;
    }
}
import { Collider, GameObject, WaitForSeconds } from 'UnityEngine';
import { ZepetoCharacter, ZepetoPlayers } from 'ZEPETO.Character.Controller';
import { ZepetoScriptBehaviour } from 'ZEPETO.Script';

export default class SpeedUp extends ZepetoScriptBehaviour 
{
    private zepetoCharacter :ZepetoCharacter;
    public runSpeed: number = 4;
    public waitTime: number = 3;

    Start() 
    {
        //Zepeto character object
        ZepetoPlayers.instance.OnAddedLocalPlayer.AddListener(() => {
            this.zepetoCharacter = ZepetoPlayers.instance.LocalPlayer.zepetoPlayer.character;
        });
    }
    
    OnTriggerEnter(collider: Collider) 
    {
        if (this.zepetoCharacter == null || collider.gameObject != this.zepetoCharacter.gameObject)
            return;
        this.zepetoCharacter.additionalRunSpeed = this.runSpeed;
        this.StartCoroutine(this.DoRoutine());
    }

    *DoRoutine() 
    {
        //Wait for waitTime value
        yield new WaitForSeconds(this.waitTime);
        //Turn the run speed back to normal
        this.zepetoCharacter.additionalRunSpeed = 0;
    }
}
import { ZepetoScriptBehaviour } from 'ZEPETO.Script';
import { Text } from "UnityEngine.UI";
import { GameObject } from 'UnityEngine';

export default class UIController extends ZepetoScriptBehaviour 
{
    public resultUI: Text;
    public maxCount: number = 7;
    
    private stringProperty: string;   
    private message: string;
    private count: number;
    
    Start() 
    {
        //Init value
        this.message = "Items";
        this.count = 0;
    }
    
    public IncreaseCount() 
    {
        this.count++;
        
        if(this.count >= this.maxCount) 
        {
            this.resultUI.text = "Clear!";
        } 
        else 
        {
            this.stringProperty = `${this.message} : ${this.count}`;
            this.resultUI.text = this.stringProperty;
        }
    }
     
}