侧视图示例
4 分
这些是设置侧视控制的示例。

步骤 1 : 相机视图设置
将相机设置为侧视图。(这是一个示例,请根据您的项目修改相机设置。)
此时,相机的标签应设置为 MainCamara。

步骤 2 : InputAction 设置
通过定义适合侧视图的新 InputAction,您可以通过触摸屏控制角色移动。
选择创建 > 输入动作,并将文件命名为侧视动作。
- 移动
- 动作类型 : 通过
- 移动触发器
- 动作类型 : 按钮

创建层级 > 创建空对象并将其重命名为侧视控制器。
从 SideViewController 对象中,选择添加组件并添加玩家输入。

拖动并连接您刚刚创建的 SideView 动作到动作列表中。
将行为更改为调用 Unity 事件。

步骤 3 : 脚本编写
创建一个脚本来控制 ZEPETO 角色从一侧移动到另一侧,并让相机跟随。
选择创建 > ZEPETO > TypeScript,并将其重命名为侧视控制器。
将脚本添加到侧视控制器对象。
import {ZepetoScriptBehaviour} from 'ZEPETO.Script'
import {PlayerInput,InputAction} from "UnityEngine.InputSystem"
import {CallbackContext} from "UnityEngine.InputSystem.InputAction"
import {CharacterState, ZepetoCharacter, ZepetoPlayers, ZepetoCamera} from 'ZEPETO.Character.Controller'
import {Camera, Quaternion, Time, Vector2, Vector3, WaitForEndOfFrame} from "UnityEngine";
export default class SideViewController extends ZepetoScriptBehaviour {
public customCamera : Camera;
public cameraDistance : number = 10;
private originSpawnPoint : Vector3 = new Vector3(-16,-5,0);
private myCharacter: ZepetoCharacter;
private startPos: Vector2 = Vector2.zero;
private curPos: Vector2 = Vector2.zero;
private playerInput: PlayerInput;
private touchPositionAction : InputAction;
private touchTriggerAction : InputAction;
private isTriggered: boolean = false;
private isTouchDown: boolean = false;
private CanMove() : boolean {
return this.isTouchDown && !this.isTriggered;
}
OnEnable(){
this.playerInput = this.gameObject.GetComponent<PlayerInput>();
}
Start() {
this.touchTriggerAction = this.playerInput.actions.FindAction("MoveTrigger");
this.touchPositionAction = this.playerInput.actions.FindAction("Move");
this.touchTriggerAction.add_started((context)=>{
this.isTriggered = true;
this.isTouchDown = true;
});
this.touchTriggerAction.add_canceled((context)=>{
this.isTriggered = false;
this.isTouchDown = false;
});
this.touchPositionAction.add_performed((context)=>{
if(this.isTouchDown)
{
this.curPos = context.ReadValueAsObject() as Vector2;
if(this.isTriggered) {
this.isTriggered = false;
this.startPos = this.curPos;
}
}
});
//关闭zepeto相机
ZepetoPlayers.instance.OnAddedLocalPlayer.AddListener(() => {
ZepetoPlayers.instance.LocalPlayer.zepetoCamera.gameObject.SetActive(false);
this.myCharacter = ZepetoPlayers.instance.LocalPlayer.zepetoPlayer.character;
this.customCamera.transform.position = this.myCharacter.transform.position + this.originSpawnPoint;
this.StartCoroutine(this.InputControlLoop());
});
}
private *InputControlLoop(){
while(true)
{
yield new WaitForEndOfFrame();
if (this.myCharacter && this.CanMove()) {
var camRot = Quaternion.Euler(0, Camera.main.transform.rotation.eulerAngles.y, 0);
var moveDir = Vector2.op_Subtraction(this.curPos, this.startPos);
moveDir = Vector2.op_Division(moveDir, 100);
if (moveDir.magnitude > 0) {
if(moveDir.magnitude > 1)
moveDir.Normalize();
//左右移动
var optMoveDir = new Vector3(moveDir.x, 0, 0);
optMoveDir = Vector3.op_Multiply(optMoveDir, Time.deltaTime * 80 );
this.myCharacter.Move(camRot * optMoveDir);
}
}
}
}
//跟随zepeto角色
LateUpdate()
{
if(null == this.myCharacter)
{
return;
}
let characterPos = this.myCharacter.transform.position;
//let cameraPosition = new Vector3(characterPos.x - this.cameraDistance, this.customCamera.transform.position.y, characterPos.z);
let cameraPosition = new Vector3(characterPos.x - this.cameraDistance, characterPos.y + 1, characterPos.z);
this.customCamera.transform.position = cameraPosition;
}
}
请在运行之前在检查器上设置相机。
