---
title: 四分之一视图示例
slug: world-sdk-guide-ch/hjDtlPYUMLY5NPNxCJAhu
docTags: 
createdAt: 2024-09-02T08:34:13.135Z
---

ZEPETO角色控制可以自定义为使用Unity的新输入系统，而不是V-Pad。

这些是设置四分之一视图控制的示例。

![](https://api.archbee.com/api/optimize/fCt3n1oCa8rgNJ8fw9I2N-xmQtwzWGSIx89Sk3SXoD--20240904-102421.gif)



## 步骤 1 : 相机视图设置

将相机设置为四分之一视图（这是一个示例，请根据您的项目修改相机设置）。

此时，相机的标签应设置为MainCamera。

![](https://api.archbee.com/api/optimize/fCt3n1oCa8rgNJ8fw9I2N-4LZZcBKXUC_vjjaBZ0NHG-20240904-102421.png)

## 步骤 2 : 输入动作设置

通过为四分之一视图定义新的输入动作，您可以通过触摸屏控制角色移动。

选择创建 > 输入动作，并将文件名设置为QuarterViewActions。

- 移动
  - 动作类型 : 通过
- 移动触发器
  - 动作类型 : 按钮

![](https://api.archbee.com/api/optimize/fCt3n1oCa8rgNJ8fw9I2N-9m15KB6FImRIffLFOQJyO-20240904-102421.png)



创建层次结构 > 创建空对象后，将文件名更改为 QuarterViewController。

从 QuarterViewController 对象中，选择添加组件并添加玩家输入。

::Image[]{src="https://api.archbee.com/api/optimize/fCt3n1oCa8rgNJ8fw9I2N-hoF1gVUhEJKbPMIEVxZVV-20240904-102421.png" size="84" width="407" height="290" position="center" showCaption="false"}



拖动并连接您刚刚创建的 QuarterView 动作到动作列表中。

将行为更改为调用 Unity 事件。

![](https://api.archbee.com/api/optimize/fCt3n1oCa8rgNJ8fw9I2N-RDg-RfMrNca4Ctr_HbZEg-20240904-102421.png)



## 步骤 3 : 脚本编写

编写一个脚本，通过触摸屏输入计算角色的移动方向。

选择 创建 > ZEPETO > TypeScript 并将其重命名为 QuarterView Controller。

将脚本添加到QuarterView Controller对象。

```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 QuarterViewController extends ZepetoScriptBehaviour {

    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;
                }
            }
        });


        ZepetoPlayers.instance.OnAddedLocalPlayer.AddListener(() => {
            ZepetoPlayers.instance.LocalPlayer.zepetoCamera.gameObject.SetActive(false);
            this.myCharacter = ZepetoPlayers.instance.LocalPlayer.zepetoPlayer.character;

            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, moveDir.y);
                    optMoveDir = Vector3.op_Multiply(optMoveDir, Time.deltaTime * 80 );
                    this.myCharacter.Move(camRot * optMoveDir);
                }
            }
        }
    }
}
```

