---
title: ตัวอย่างมุมมองด้านข้าง
slug: world-sdk-guide-th/sqEHHcPQ2fUU5aM6V-OZp
docTags: 
createdAt: 2024-09-02T08:34:13.135Z
---

นี่คือตัวอย่างการตั้งค่าการควบคุมมุมมองด้านข้าง.

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



## ขั้นตอนที่ 1 : การตั้งค่ามุมมองกล้อง

ตั้งค่ากล้องให้เป็นมุมมองด้านข้าง (นี่คือตัวอย่าง ดังนั้นโปรดปรับการตั้งค่ากล้องตามโครงการของคุณ.)

ในจุดนี้ แท็กของกล้องควรตั้งค่าเป็น MainCamara.

![](https://api.archbee.com/api/optimize/fCt3n1oCa8rgNJ8fw9I2N-8hIVR2bWbNjllEL_fHRpg-20240904-102410.png)



## ขั้นตอนที่ 2 : การตั้งค่า InputAction

โดยการกำหนด InputAction ใหม่ที่เหมาะสมกับมุมมองด้านข้าง คุณสามารถควบคุมการเคลื่อนไหวของตัวละครผ่านหน้าจอสัมผัส.

เลือก สร้าง > การกระทำของ Input และตั้งชื่อไฟล์ว่า SideView Actions.

- เคลื่อนที่
  - ประเภทการกระทำ : ผ่านเข้าไป
- เคลื่อนที่ Trigger
  - ประเภทการกระทำ : ปุ่ม

![](https://api.archbee.com/api/optimize/fCt3n1oCa8rgNJ8fw9I2N-RwVihcfjK9LGbys6lsC6l-20240904-102410.png)



สร้างลำดับชั้น > สร้างวัตถุเปล่าและเปลี่ยนชื่อเป็น SideView Controller.

จากวัตถุ SideViewController ให้เลือก เพิ่มส่วนประกอบ และเพิ่ม Player Input.

![](https://api.archbee.com/api/optimize/fCt3n1oCa8rgNJ8fw9I2N-wo83zvFZ7pwkZeSBViwT--20240904-102410.png)



ลากและเชื่อมต่อ SideView Actions ที่คุณเพิ่งสร้างไปยังรายการ Actions.

เปลี่ยนพฤติกรรมเป็นการเรียกใช้ Unity Events.

![](https://api.archbee.com/api/optimize/fCt3n1oCa8rgNJ8fw9I2N-QQhgAXzQh92CAa3L2Cmnd-20240904-102410.png)



## ขั้นตอนที่ 3 : การเขียนสคริปต์

สร้างสคริปต์เพื่อควบคุมตัวละคร ZEPETO ให้เคลื่อนที่ไปมา และให้กล้องติดตาม.

เลือก สร้าง > ZEPETO > TypeScript และเปลี่ยนชื่อเป็น SideView Controller.

เพิ่มสคริปต์ไปยังวัตถุ SideViewController.

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

        //turn off zepeto camera
        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();

                    //left-right
                    var optMoveDir = new Vector3(moveDir.x, 0, 0);
                    optMoveDir = Vector3.op_Multiply(optMoveDir, Time.deltaTime * 80 );
                    this.myCharacter.Move(camRot * optMoveDir);
                }
            }
        }
    }

    //follow zepeto character
    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;
    }
}
```



กรุณาตั้งค่ากล้องในผู้ตรวจสอบก่อนที่จะรันมัน.

![](https://api.archbee.com/api/optimize/fCt3n1oCa8rgNJ8fw9I2N-cxIFq5pFsEfq-1K9ugvXW-20240904-102410.png)

