创造你的世界
玩家及角色:进阶
侧视图示例
4min
这些是设置侧视控制的示例。
将相机设置为侧视图。(这是一个示例,请根据您的项目修改相机设置。)
此时,相机的标签应设置为 MainCamara。
通过定义适合侧视图的新 InputAction,您可以通过触摸屏控制角色移动。
选择创建 > 输入动作,并将文件命名为侧视动作。
- 移动
- 动作类型 : 通过
- 移动触发器
- 动作类型 : 按钮
创建层级 > 创建空对象并将其重命名为侧视控制器。
从 SideViewController 对象中,选择添加组件并添加玩家输入。
拖动并连接您刚刚创建的 SideView 动作到动作列表中。
将行为更改为调用 Unity 事件。
创建一个脚本来控制 ZEPETO 角色从一侧移动到另一侧,并让相机跟随。
选择创建 > ZEPETO > TypeScript,并将其重命名为侧视控制器。
将脚本添加到侧视控制器对象。
TypeScript
1import {ZepetoScriptBehaviour} from 'ZEPETO.Script'
2import {PlayerInput,InputAction} from "UnityEngine.InputSystem"
3import {CallbackContext} from "UnityEngine.InputSystem.InputAction"
4import {CharacterState, ZepetoCharacter, ZepetoPlayers, ZepetoCamera} from 'ZEPETO.Character.Controller'
5import {Camera, Quaternion, Time, Vector2, Vector3, WaitForEndOfFrame} from "UnityEngine";
6
7export default class SideViewController extends ZepetoScriptBehaviour {
8
9 public customCamera : Camera;
10 public cameraDistance : number = 10;
11
12 private originSpawnPoint : Vector3 = new Vector3(-16,-5,0);
13 private myCharacter: ZepetoCharacter;
14 private startPos: Vector2 = Vector2.zero;
15 private curPos: Vector2 = Vector2.zero;
16
17 private playerInput: PlayerInput;
18 private touchPositionAction : InputAction;
19 private touchTriggerAction : InputAction;
20
21 private isTriggered: boolean = false;
22 private isTouchDown: boolean = false;
23
24 private CanMove() : boolean {
25 return this.isTouchDown && !this.isTriggered;
26 }
27
28
29 OnEnable(){
30 this.playerInput = this.gameObject.GetComponent<PlayerInput>();
31 }
32
33 Start() {
34
35 this.touchTriggerAction = this.playerInput.actions.FindAction("MoveTrigger");
36 this.touchPositionAction = this.playerInput.actions.FindAction("Move");
37
38 this.touchTriggerAction.add_started((context)=>{
39 this.isTriggered = true;
40 this.isTouchDown = true;
41 });
42
43 this.touchTriggerAction.add_canceled((context)=>{
44 this.isTriggered = false;
45 this.isTouchDown = false;
46 });
47
48 this.touchPositionAction.add_performed((context)=>{
49
50 if(this.isTouchDown)
51 {
52 this.curPos = context.ReadValueAsObject() as Vector2;
53
54 if(this.isTriggered) {
55 this.isTriggered = false;
56 this.startPos = this.curPos;
57 }
58 }
59 });
60
61 //关闭zepeto相机
62 ZepetoPlayers.instance.OnAddedLocalPlayer.AddListener(() => {
63 ZepetoPlayers.instance.LocalPlayer.zepetoCamera.gameObject.SetActive(false);
64 this.myCharacter = ZepetoPlayers.instance.LocalPlayer.zepetoPlayer.character;
65 this.customCamera.transform.position = this.myCharacter.transform.position + this.originSpawnPoint;
66 this.StartCoroutine(this.InputControlLoop());
67 });
68 }
69
70 private *InputControlLoop(){
71 while(true)
72 {
73 yield new WaitForEndOfFrame();
74
75 if (this.myCharacter && this.CanMove()) {
76
77 var camRot = Quaternion.Euler(0, Camera.main.transform.rotation.eulerAngles.y, 0);
78 var moveDir = Vector2.op_Subtraction(this.curPos, this.startPos);
79 moveDir = Vector2.op_Division(moveDir, 100);
80
81
82 if (moveDir.magnitude > 0) {
83
84 if(moveDir.magnitude > 1)
85 moveDir.Normalize();
86
87 //左右移动
88 var optMoveDir = new Vector3(moveDir.x, 0, 0);
89 optMoveDir = Vector3.op_Multiply(optMoveDir, Time.deltaTime * 80 );
90 this.myCharacter.Move(camRot * optMoveDir);
91 }
92 }
93 }
94 }
95
96 //跟随zepeto角色
97 LateUpdate()
98 {
99 if(null == this.myCharacter)
100 {
101 return;
102 }
103 let characterPos = this.myCharacter.transform.position;
104 //let cameraPosition = new Vector3(characterPos.x - this.cameraDistance, this.customCamera.transform.position.y, characterPos.z);
105 let cameraPosition = new Vector3(characterPos.x - this.cameraDistance, characterPos.y + 1, characterPos.z);
106 this.customCamera.transform.position = cameraPosition;
107 }
108}
请在运行之前在检查器上设置相机。