---
title: ตัวละคร ZEPETO
slug: world-sdk-guide-th/msDX-zepeto
docTags: 
createdAt: 2024-09-02T08:34:13.135Z
---

ZEPETOCharacter เป็นหน่วยพื้นฐานของตัวละคร ZEPETO สำหรับโหลดใน World Scene.

ในการควบคุม ZepetoCharacter ให้เพิ่มคำสั่งนำเข้าต่อไปนี้ในสคริปต์.

```typescript
import { ZepetoCharacter } from 'ZEPETO.Character.Controller';
```



## ZepetoCharacter API

API นี้สร้างและลบตัวละคร ZEPETO รวมถึงคอนโทรลเลอร์อนิเมเตอร์และคอนโทรลเลอร์ตัวละคร.

| API                                                                                 | คำอธิบาย                                                                        |
| ----------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- |
| CreateByZepetoId(zepetoId: string, spawnInfo: SpawnInfo, complete: System.Action$1) | ใช้ ZEPETO ID เพื่อสร้างตัวละคร ZEPETO โดยหลักแล้วใช้สำหรับการสร้างตัวละคร NPC. |
| CreateByUserId(userId: string, spawnInfo: SpawnInfo, complete: System.Action$1)     | สร้างตัวละคร ZEPETO โดยใช้ UserId โดยหลักแล้วใช้สำหรับการสร้างตัวละคร NPC.      |
| RemoveCharacter(character: ZepetoCharacter)                                         | ลบตัวละครออกจากอินสแตนซ์.                                                       |



API นี้สร้างอินสแตนซ์โมเดลตัวละคร ZEPETO โดยไม่รวมคอนโทรลเลอร์อนิเมเตอร์และคอนโทรลเลอร์ตัวละคร.

:::hint{type="success"}
- ZEPETO ID : นี่คือค่ารหัสที่ผู้ใช้ระบุโดยตรงและใช้ภายในแอป ZEPETO.
- User ID : นี่คือค่ารหัสที่ไม่ซ้ำกันซึ่งแยกแยะผู้ใช้ภายในระบบ ZEPETO และไม่ใช่ค่าที่แสดงบน UI คุณสามารถตรวจสอบได้โดยใช้สคริปต์.
:::



หากคุณสนใจใน ZepetoCharacter API โปรดดูเอกสาร:

:::hint{type="info"}
- โปรดดูคู่มือต่อไปนี้. \[[ZEPETO.Character.Controller API](https://developer.zepeto.me/docs/character-controller/)]
:::



## สร้าง/ลบตัวละคร

```typescript
import { ZepetoScriptBehaviour } from 'ZEPETO.Script';
import { SpawnInfo, ZepetoCharacter, ZepetoCharacterCreator } from 'ZEPETO.Character.Controller';
import { Button } from 'UnityEngine.UI';
import { GameObject, Object, Vector3 } from 'UnityEngine';
import { WorldService } from 'ZEPETO.World';
 
export default class SampleScript extends ZepetoScriptBehaviour {
 
    public zepetoId: string;
    public removeCloneCharacterButton : Button;
    public removeCloneCharacterModelButton : Button;
 
    private _cloneCharacter : ZepetoCharacter;
    private _cloneCharacterModel : GameObject;
 
    Start() {
        // สร้างอินสแตนซ์ใหม่ของคลาส SpawnInfo
        const firstCloneSpawnInfo = new SpawnInfo();
        firstCloneSpawnInfo.position = new Vector3(0,0,1);
 
        const secondCloneSpawnInfo = new SpawnInfo();
        secondCloneSpawnInfo.position = new Vector3(0,0,3);
 
 
 
        // สร้าง `ZepetoCharacter` โดยใช้ ZepetoId
        ZepetoCharacterCreator.CreateByZepetoId(this.zepetoId, firstCloneSpawnInfo, (character: ZepetoCharacter) => {
            this._cloneCharacter = character;
        });
 
 
        // สร้าง `ZepetoCharacterModel` โดยใช้ ZepetoId
        ZepetoCharacterCreator.CreateModelByZepetoId(this.zepetoId, secondCloneSpawnInfo, (object) => {
            this._cloneCharacterModel = object;
        });
 
        // สร้าง `ZepetoCharacter` โดยใช้ UserId
        /*
        ZepetoCharacterCreator.CreateByUserId(WorldService.userId, new SpawnInfo(), (character: ZepetoCharacter) => {
            this._cloneCharacter = character;
        })
  
        // สร้าง `ZepetoCharacterModel` โดยใช้ UserId
        ZepetoCharacterCreator.CreateModelByUserId(WorldService.userId, new SpawnInfo(), (object) => {
            this._cloneCharacterModel = object;
        })
        */
 
        // ตั้งค่ากิจกรรมคลิกสำหรับ `removeCloneCharacterButton` เพื่อลบตัวละครโคลน
        this.removeCloneCharacterButton.onClick.AddListener(() => {
            ZepetoCharacterCreator.RemoveCharacter(this._cloneCharacter);
        });
 
        // ตั้งค่ากิจกรรมคลิกสำหรับ `removeCloneCharacterModelButton` เพื่อลบโมเดลตัวละครโคลน
        this.removeCloneCharacterModelButton.onClick.AddListener(() => {
            Object.Destroy(this._cloneCharacterModel);
        });
    }
 
}
```



ถ้าคุณรันตัวอย่าง คุณจะเห็นว่า CharacterModel ทางซ้ายถูกสร้างขึ้นโดยไม่มี Animator Controller และ Character Controller ตามที่แสดงด้านล่าง

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



## ควบคุมตัวละคร

จะมีอินเทอร์เฟซที่คุณสามารถควบคุมตัวละครได้โดยตรงผ่านสคริปต์จากตัวละครในฉาก.

| **API**                                                                   | **คำอธิบาย**                                                                                                                                                                                                                                         |
| ------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| MoveToPosition(position : Vector3)                                        | ย้ายตัวละครไปยังตำแหน่งที่กำหนด                                                                                                                                                                                                                      |
| MoveContinuously(direction : Vector3)                                     | เคลื่อนที่อย่างต่อเนื่อง (อัปเดต) ในทิศทางที่ตัวละครมองไป                                                                                                                                                                                            |
| MoveContinuously(direction : Vector2)                                     | เคลื่อนที่อย่างต่อเนื่อง (อัปเดต) ในทิศทางที่ตัวละครมองไป                                                                                                                                                                                            |
| StopMoving()                                                              | หยุดการเคลื่อนที่ของตัวละคร                                                                                                                                                                                                                          |
| Jump()                                                                    | ตัวละครกระโดดไปยังพลังการกระโดดที่ตั้งไว้                                                                                                                                                                                                            |
| DoubleJump()                                                              | ตัวละครกระโดดสองครั้งด้วยพลังการกระโดดสองครั้งที่ตั้งไว้ (ใช้ได้เฉพาะเมื่อใช้ MotionController V2)                                                                                                                                                   |
| Teleport(position: UnityEngine.Vector3, rotation: UnityEngine.Quaternion) | ตัวละครเคลื่อนที่ไปยัง Transform ทันที                                                                                                                                                                                                               |
| SetGesture(gesture: UnityEngine.AnimationClip)                            | สำหรับ AnimationClip ที่กำหนด การเคลื่อนไหวของตัวละครจะถูกเล่น<br /><br />ในขณะที่ SetGesture กำลังทำงาน การควบคุมของผู้ใช้จะไม่ถูกนำไปใช้กับตัวละคร<br /><br />เมื่อเปิดตัวเลือก loop ของ AnimationClip มันจะเล่นต่อไปจนกว่าจะเรียก CancelGesture() |
| CancelGesture()                                                           | หยุดการเล่น Animation Clip ที่กำลังเล่นอยู่ผ่าน SetGesture()<br /><br />เมื่อ CancelGesture() ถูกเรียก การควบคุมของผู้ใช้จะถูกนำไปใช้กับการควบคุมตัวละครอีกครั้ง                                                                                     |

### ตัวอย่างโค้ดตัวควบคุมอักขระ

```typescript
import { ZepetoScriptBehaviour } from 'ZEPETO.Script'
import {SpawnInfo, ZepetoCharacter, ZepetoCharacterCreator} from "ZEPETO.Character.Controller";
import { WorldService } from 'ZEPETO.World';
import {AnimationClip, Vector3, WaitForSeconds } from 'UnityEngine';
 
export default class CharacterActionSample extends ZepetoScriptBehaviour {
 
    public targetPosition: Vector3;
    public danceGesture: AnimationClip;
    private _cloneCharacter: ZepetoCharacter;
 
    Start() {
 
        // สร้าง `ZepetoCharacter` โดยใช้ `ZepetoCharacterCreator.CreateByUserId`.
        ZepetoCharacterCreator.CreateByUserId(WorldService.userId, new SpawnInfo(), (character: ZepetoCharacter) => {
            // กำหนดตัวละครที่สร้างขึ้นให้กับ `_cloneCharacter`.
            this._cloneCharacter = character;
 
            // เรียกใช้ `ActionCoroutine()` เป็น coroutine.
            this.StartCoroutine(this.ActionCoroutine());
        })
    }
 
    *ActionCoroutine() {
 
        yield new WaitForSeconds(3);
 
        // ย้าย `_cloneCharacter` ไปยัง `targetPosition` หลังจากรอ 3 วินาที.
        this._cloneCharacter.MoveToPosition(this.targetPosition);
 
        yield new WaitForSeconds(1);
 
        // ให้ `_cloneCharacter` กระโดดหลังจากรอ 1 วินาที.
        this._cloneCharacter.Jump();
 
        yield new WaitForSeconds(1);
 
        // ตั้งท่าทางของ `_cloneCharacter` เป็น `danceGesture` หลังจากรอ 1 วินาที.
        this._cloneCharacter.SetGesture(this.danceGesture);
 
        yield new WaitForSeconds(3);
 
        // ยกเลิกท่าทางของ `_cloneCharacter` หลังจากรอ 3 วินาที.
        this._cloneCharacter.CancelGesture();
    }
 
}
```



หากคุณรันตัวอย่าง คุณจะเห็นว่าตัวละครที่ถูกโคลนทำการกระทำที่กำหนดไว้ตามลำดับดังนี้

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

:::hint{type="info"}
**📘&#x20;**&#xE01;รุณาอ้างอิงจากคู่มือต่อไปนี้ \[[การสร้างและการควบคุม NPC](docId\:i_hRnbtqowhm4KgGjzs5r)]
:::



## สถานะการเคลื่อนไหว

API ที่เกี่ยวข้องกับสถานะการเคลื่อนไหวของตัวละคร ZEPETO มีดังนี้:

| **API**                                  | **คำอธิบาย**                                                                                                                           |
| ---------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------- |
| motionState.useDoubleJump = (boolean);   | ตั้งค่าว่าตัวละครสามารถใช้ DoubleJump ได้หรือไม่.                                                                                      |
| motionState.doubleJumpPower = (number);  | ตั้งค่าพลัง DoubleJump ของตัวละคร.                                                                                                     |
| motionState.useLandingRoll = (boolean);  | ตั้งค่าว่าจะใช้ LandingRoll ของตัวละครหรือไม่.                                                                                         |
| motionState.landingRollSpeed = (number); | ตั้งค่าความเร็วของ LandingRoll ของตัวละคร.                                                                                             |
| motionState.useMoveTurn = (boolean);     | ตั้งค่าว่าจะใช้ MoveTurn สำหรับตัวละครหรือไม่.                                                                                         |
| motionState.Gravity = (number);          | ตั้งค่าค่าความโน้มถ่วงของตัวละคร.                                                                                                      |
| motionState.CurrentJumpState             | คุณสามารถตรวจสอบสถานะการกระโดดปัจจุบันของตัวละครได้.<br /><br />- ไม่มี = -1, JumpIdle = 0, JumpMove = 1, JumpDash = 2, JumpDouble = 3 |
| motionState.CurrentLandingState          | คุณสามารถตรวจสอบสถานะการลงจอดปัจจุบันของตัวละครได้.<br /><br />- ไม่มี = -1, LandingSlight = 0, LandingDeep = 1, LandingRoll = 2       |
| motionState.CurrentMoveState             | คุณสามารถตรวจสอบสถานะการเคลื่อนไหวปัจจุบันของตัวละครได้.<br /><br />- ไม่มี = -1, MoveWalk = 0, MoveRun = 1                            |

### ตัวอย่างโค้ดสถานะการเคลื่อนไหว

```typescript
import { ZepetoScriptBehaviour } from 'ZEPETO.Script'
import { CustomMotionData, SpawnInfo, ZepetoCharacter, ZepetoCharacterCreator, ZepetoPlayer, LocalPlayer, ZepetoPlayers } from "ZEPETO.Character.Controller";
import { WorldService } from 'ZEPETO.World';
import { Button } from 'UnityEngine.UI';
 
export default class MotionSample extends ZepetoScriptBehaviour {
     
    public jumpButton : Button;
    private _cloneCharacter: ZepetoCharacter;
 
    Start() {
        ZepetoCharacterCreator.CreateByUserId(WorldService.userId, new SpawnInfo(), (character: ZepetoCharacter) => {
            // กำหนดตัวละครที่สร้างขึ้นให้กับ `_cloneCharacter`.
            this._cloneCharacter = character;
 
            // ตั้งค่าคุณสมบัติต่างๆ ของ `motionState` ใน `_cloneCharacter`.
            this._cloneCharacter.motionState.useDoubleJump = true;
            this._cloneCharacter.motionState.useDoubleJump = true;
            this._cloneCharacter.motionState.useLandingRoll = true;
            this._cloneCharacter.motionState.useMoveTurn = true;
            this._cloneCharacter.motionState.gravity = 1;
        })
         
        this.jumpButton.onClick.AddListener(() => {
            this._cloneCharacter.Jump();
        });
    }
 
    Update() {
        // ตรวจสอบว่า `_cloneCharacter` ไม่เป็น null.
        if (this._cloneCharacter != null) {
            // พิมพ์สถานะการกระโดดปัจจุบันของ `motionState` ใน `_cloneCharacter`.
            console.log(`สถานะการกระโดดปัจจุบัน: ${this._cloneCharacter.motionState.currentJumpState}`);
 
            // พิมพ์สถานะการลงจอดปัจจุบันของ `motionState` ใน `_cloneCharacter`.
            console.log(`สถานะการลงจอดปัจจุบัน: ${this._cloneCharacter.motionState.currentLandingState}`);
 
            // พิมพ์สถานะการเคลื่อนไหวปัจจุบันของ `motionState` ใน `_cloneCharacter`.
            console.log(`สถานะการเคลื่อนไหวปัจจุบัน: ${this._cloneCharacter.motionState.currentMoveState}`);
        }
    }
}
```



## การควบคุมแอนิเมเตอร์ตัวละคร ZEPETO

### API สำหรับควบคุมแอนิเมเตอร์ตัวละคร ZEPETO

| **API**                               | **คำอธิบาย**                                                                                                                                                                                                                                                                                  |
| ------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| StateMachine.constraintStateAnimation | เริ่มตั้งแต่เวอร์ชัน 1.6.0 ของ ZEPETO.World การเปลี่ยนแปลงของ Animation ที่เล่นใน CharacterStateMachine สามารถเปิด/ปิดได้ตาม CharacterState ของ Motion V2.<br /><br />- true : ปิด ZepetoCharacter.CurrentState ซึ่งได้รับผลกระทบจากการป้อนข้อมูลของตัวควบคุม และควบคุมคลิปอนิเมชันที่ต้องการ |

:::hint{type="success"}
👍 **ตัวอย่างการใช้ constraintStateAnimation**

- คุณต้องการให้ตัวละครของคุณใช้อนิเมชันการว่ายน้ำแทนการเดิน คุณสามารถใช้คลิปอนิเมชันการว่ายน้ำกับ State ใหม่และแก้ไข State ผ่าน constraintStateAnimation.
- หากคุณต้องการใช้อนิเมชันท่าทางขณะเคลื่อนที่ตัวละคร คุณสามารถใช้คลิปอนิเมชันท่าทางกับ State ใหม่และเคลื่อนที่ตัวละครด้วย State ที่แก้ไขโดยใช้ constraintStateAnimation.
- สิ่งนี้สามารถใช้ได้เมื่อคุณต้องการเล่นอนิเมชันเฉพาะขณะละเลยค่าพารามิเตอร์ที่ใช้กับ Animator State ชั่วคราว.
:::



### ZepetoAnimator

ฟังก์ชัน Animator ของ UnityEngine มีให้ใช้งานในรูปแบบของ ZepetoAnimator.

ตัวอย่างเช่น หากคุณต้องการตั้งค่า Integer สำหรับพารามิเตอร์ "State" ของ Character Animator เฉพาะ คุณสามารถใช้:

```typescript
ZepetoAnimator.SetInteger("State", 1);
```

สำหรับการใช้งานที่ละเอียดมากขึ้น โปรดดูเอกสารของ UnityEngine Animator และสคริปต์ตัวอย่างด้านล่าง.

:::hint{type="info"}
**📘 UnityEngine Animator**
[https://docs.unity3d.com/ScriptReference/Animator.html](https://docs.unity3d.com/ScriptReference/Animator.html)
:::



### ตัวอย่างโค้ดสำหรับควบคุม ZEPETO Character Animator

ตัวอย่างการควบคุม Animator ของตัวละคร ZEPETO ที่เล่นโดยผู้เล่น.

```typescript
import { Toggle } from 'UnityEngine.UI';
import { SpawnInfo, ZepetoCharacter, ZepetoPlayers } from 'ZEPETO.Character.Controller';
import { ZepetoScriptBehaviour } from 'ZEPETO.Script'
import { WorldService } from 'ZEPETO.World';
import { Animator } from "UnityEngine";
 
export default class SampleScript extends ZepetoScriptBehaviour {
    public testTogle: Toggle;
    private _zepetoCharacter: ZepetoCharacter;
    Start() {
        // Local Player Creation Code (Please delete this section if you are already using local player creation code elsewhere)                        ZepetoPlayers.instance.CreatePlayerWithUserId(WorldService.userId, new SpawnInfo(), true);
 
        // Code for setting the created local player as a ZEPETO character.
          ZepetoPlayers.instance.OnAddedLocalPlayer.AddListener(() => {
            this._zepetoCharacter = ZepetoPlayers.instance.LocalPlayer.zepetoPlayer.character;
 
            // SetInteger Example
            const stateType = Animator.StringToHash("State");
            this._zepetoCharacter.ZepetoAnimator.SetInteger(stateType, 30);
 
            // StateMachine.constraintStateAnimation
            this.testTogle.onValueChanged.AddListener((isActive: bool)=>{
                this._zepetoCharacter.StateMachine.constraintStateAnimation = isActive;
                console.log(isActive);
            });
        });
    }
}
```



ถ้าคุณรันตัวอย่าง คุณจะเห็นว่าเมื่อค่าของ StateMachine.constraintStateAnimation เป็น True การเปลี่ยนแปลงของ Animation ที่เล่นใน CharacterStateMachine จะเป็น Off.

ถ้าคุณตั้งค่าของ StateMachine.constraintStateAnimation เป็น False มันจะกลับไปสู่สถานะเดิม.

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



## การควบคุม CharacterShadow

เริ่มตั้งแต่เวอร์ชัน 1.11.3 ของ ZEPETO.CharacterController Package ได้มีการเพิ่มอินเตอร์เฟซเพื่อเข้าถึงวัตถุ ZepetoCharacter Shadow.

ข้อกำหนด API:

:::CodeblockTabs
ส่วนติดต่อของคอมโพเนนต์ CharacterShadow

```typescript
class CharacterShadow extends UnityEngine.MonoBehaviour {
     
    public target: UnityEngine.Transform;
    public autoSyncTransform: boolean;
                
}
```
:::

| เป้าหมาย          | - แสดงถึงวัตถุที่ทำหน้าที่เป็นการอ้างอิงสำหรับการปรับเปลี่ยนการแปลงของวัตถุ CharacterShadow<br />- โดยค่าเริ่มต้น ตัวละคร ZEPETO จะถูกตั้งเป็นเป้าหมาย การแปลงของวัตถุ CharacterShadow จะถูกปรับตามด้านล่างของตัวละครที่ระบุ |
| ----------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| autoSyncTransform | - ค่าธงที่กำหนดว่าจะแก้ไขตำแหน่งของเงาหรือไม่<br />- ค่าเริ่มต้นคือ true.                                                                                                                                                    |

ด้วยสิ่งนี้ คุณสามารถควบคุมเงาของตัวละคร Zepeto ในระหว่างการทำงานได้ ด้านล่างนี้คือตัวอย่างโค้ดที่ใช้สวิตช์เพื่อเปิดและปิดเงา

```typescript
import { ZepetoScriptBehaviour } from 'ZEPETO.Script'
import { ZepetoCharacter, CharacterShadow, ZepetoPlayer, ZepetoPlayers } from 'ZEPETO.Character.Controller';
import { Toggle } from 'UnityEngine.UI';
 
export default class ShadowController extends ZepetoScriptBehaviour {
 
    public shadowToggle : Toggle;
 
    private _characterShadow : CharacterShadow;
    private _localCharacter: ZepetoCharacter;
     
    Start() {
 
        ZepetoPlayers.instance.OnAddedLocalPlayer.AddListener(() => {
            this._localCharacter = ZepetoPlayers.instance.LocalPlayer.zepetoPlayer.character;
            this._characterShadow = this._localCharacter.Context.GetComponentInChildren<CharacterShadow>();
            console.log(this._characterShadow.autoSyncTransform);
        });
         
        this.shadowToggle.onValueChanged.AddListener((isActive: bool)=>{
            this._characterShadow.gameObject.SetActive(isActive);
        });
    }
}
```



::Image[]{src="https://api.archbee.com/api/optimize/fCt3n1oCa8rgNJ8fw9I2N-64hnCm9hLy9fuqdNyzg7y-20240904-102414.gif" size="92" width="600" height="326" position="center" showCaption="false"}

