教程
制作一个可以吃东西的世界
7min
总结 | 基于我们在第一章中学到的内容,我们将使用角色移动速度和跳跃控制制作一个吃东西的世界。 |
---|---|
难度 | 初学者 |
所需时间 | 30分钟 |
项目目标
- 使用 OnTrigger 事件处理物品获取
- 了解如何应用文本用户界面
- 创建用户界面控制器以在文本用户界面中反映物品获取
- 了解如何控制 ZEPETO 角色的跳跃高度
- 了解如何控制 ZEPETO 角色的移动速度
🎬 完成世界的视频
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 //获取用户ID(通过编辑器登录)
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// 从路径导入自定义脚本
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角色对象
17 ZepetoPlayers.instance.OnAddedLocalPlayer.AddListener(() => {
18 this.zepetoCharacter = ZepetoPlayers.instance.LocalPlayer.zepetoPlayer.character;
19 });
20 //脚本导入
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 //发送吃掉物品后增加计数的消息
31 this.extComponent.IncreaseCount();
32 //销毁物品
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角色对象
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 //增加跳跃力
24 this.zepetoCharacter.additionalJumpPower = this.jumpPower;
25 }
26
27 OnTriggerExit(collider: Collider) {
28 //将跳跃力恢复到正常
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角色对象
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 //等待waitTime值
30 yield new WaitForSeconds(this.waitTime);
31 //将跑步速度恢复到正常
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 //初始化值
17 this.message = "物品";
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 = "清除!";
28 }
29 else
30 {
31 this.stringProperty = `${this.message} : ${this.count}`;
32 this.resultUI.text = this.stringProperty;
33 }
34 }
35
36}