TUTORIALS
Make an item eating World
7min
📘 Project files used in lecture
Summary | Based on what we learned in Chapter 1, we will make an item eating world using character movement speed and jump control. |
---|---|
Difficulty | Beginner |
Time Required | 30 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
CharacterController.ts
1import { ZepetoScriptBehaviour } from 'ZEPETO.Script'
2import { SpawnInfo, ZepetoPlayers, LocalPlayer, ZepetoCharacter } from 'ZEPETO.Character.Controller'
3import { WorldService } from 'ZEPETO.World';
4export default class CharacterController extends ZepetoScriptBehaviour {
5 Start() {
6 //Gets the UserId (logged in through Editor)
7 ZepetoPlayers.instance.CreatePlayerWithUserId(WorldService.userId,new SpawnInfo(), true);
8 ZepetoPlayers.instance.OnAddedLocalPlayer.AddListener(() => {
9 let _player : LocalPlayer = ZepetoPlayers.instance.LocalPlayer;
10 });
11 }
12}
GetItem.ts
1import { Collider, Vector3, GameObject, Transform } from 'UnityEngine';
2import { ZepetoCharacter, ZepetoPlayer, ZepetoPlayers } from 'ZEPETO.Character.Controller';
3import { ZepetoScriptBehaviour, ZepetoScriptInstance } from 'ZEPETO.Script';
4
5// import custom script from path
6import UIController from './UIController';
7
8export default class GetItem extends ZepetoScriptBehaviour
9{
10 public extComponentGameObject: GameObject;
11 private extComponent: UIController;
12 private zepetoCharacter: ZepetoCharacter;
13
14 Start()
15 {
16 //Zepeto character object
17 ZepetoPlayers.instance.OnAddedLocalPlayer.AddListener(() => {
18 this.zepetoCharacter = ZepetoPlayers.instance.LocalPlayer.zepetoPlayer.character;
19 });
20 //script import
21 this.extComponent = this.extComponentGameObject.GetComponent<UIController>();
22 }
23
24
25 OnTriggerEnter(collider: Collider)
26 {
27 if (this.zepetoCharacter == null || collider.gameObject != this.zepetoCharacter.gameObject)
28 return;
29
30 //Sends a message of raising a count after eating an item
31 this.extComponent.IncreaseCount();
32 //Destroy item
33 GameObject.Destroy(this.gameObject);
34 }
35}
JumpPower.ts
1import { Collider } from 'UnityEngine';
2import { ZepetoPlayers, ZepetoCharacter } from 'ZEPETO.Character.Controller';
3import { ZepetoScriptBehaviour } from 'ZEPETO.Script';
4
5export default class JumpPower extends ZepetoScriptBehaviour
6{
7 public jumpPower: number = 5;
8 private zepetoCharacter :ZepetoCharacter;
9
10 Start()
11 {
12 //Zepeto character object
13 ZepetoPlayers.instance.OnAddedLocalPlayer.AddListener(() => {
14 this.zepetoCharacter = ZepetoPlayers.instance.LocalPlayer.zepetoPlayer.character;
15 });
16 }
17
18 OnTriggerEnter(collider: Collider)
19 {
20 if (this.zepetoCharacter == null || collider.gameObject != this.zepetoCharacter.gameObject)
21 return;
22
23 //Add Jumppower
24 this.zepetoCharacter.additionalJumpPower = this.jumpPower;
25 }
26
27 OnTriggerExit(collider: Collider) {
28 //Turn the jump power back to normal
29 this.zepetoCharacter.additionalJumpPower = 0;
30 }
31}
SpeedUp.ts
1import { Collider, GameObject, WaitForSeconds } from 'UnityEngine';
2import { ZepetoCharacter, ZepetoPlayers } from 'ZEPETO.Character.Controller';
3import { ZepetoScriptBehaviour } from 'ZEPETO.Script';
4
5export default class SpeedUp extends ZepetoScriptBehaviour
6{
7 private zepetoCharacter :ZepetoCharacter;
8 public runSpeed: number = 4;
9 public waitTime: number = 3;
10
11 Start()
12 {
13 //Zepeto character object
14 ZepetoPlayers.instance.OnAddedLocalPlayer.AddListener(() => {
15 this.zepetoCharacter = ZepetoPlayers.instance.LocalPlayer.zepetoPlayer.character;
16 });
17 }
18
19 OnTriggerEnter(collider: Collider)
20 {
21 if (this.zepetoCharacter == null || collider.gameObject != this.zepetoCharacter.gameObject)
22 return;
23 this.zepetoCharacter.additionalRunSpeed = this.runSpeed;
24 this.StartCoroutine(this.DoRoutine());
25 }
26
27 *DoRoutine()
28 {
29 //Wait for waitTime value
30 yield new WaitForSeconds(this.waitTime);
31 //Turn the run speed back to normal
32 this.zepetoCharacter.additionalRunSpeed = 0;
33 }
34}
UIController.ts
1import { ZepetoScriptBehaviour } from 'ZEPETO.Script';
2import { Text } from "UnityEngine.UI";
3import { GameObject } from 'UnityEngine';
4
5export default class UIController extends ZepetoScriptBehaviour
6{
7 public resultUI: Text;
8 public maxCount: number = 7;
9
10 private stringProperty: string;
11 private message: string;
12 private count: number;
13
14 Start()
15 {
16 //Init value
17 this.message = "Items";
18 this.count = 0;
19 }
20
21 public IncreaseCount()
22 {
23 this.count++;
24
25 if(this.count >= this.maxCount)
26 {
27 this.resultUI.text = "Clear!";
28 }
29 else
30 {
31 this.stringProperty = `${this.message} : ${this.count}`;
32 this.resultUI.text = this.stringProperty;
33 }
34 }
35
36}
Updated 10 Oct 2024
Did this page help you?