---
title: 测量你角色的身高
slug: world-sdk-guide-ch/MocP6RyTiuBA9RJwF52vT
docTags: 
createdAt: 2024-09-02T08:34:13.135Z
---

在本指南中，我们将向您展示如何根据身体类型测量本地玩家的ZEPETO角色的高度。



### 步骤 1 : 编写脚本以获取角色的高度值

将一个GameObject添加到场景中，并添加下面的CharacterHeight示例脚本。

```typescript
import { Debug, HumanBodyBones, Vector3, WaitForEndOfFrame } from 'UnityEngine';
import { LocalPlayer, ZepetoPlayers, ZepetoPlayer, KnowSockets } from 'ZEPETO.Character.Controller';
import { ZepetoScriptBehaviour } from 'ZEPETO.Script';

export default class CharacterHeight extends ZepetoScriptBehaviour {

    Start() {    
        ZepetoPlayers.instance.OnAddedLocalPlayer.AddListener(() => {
            let _player: LocalPlayer = ZepetoPlayers.instance.LocalPlayer;
            this.StartCoroutine(this.CoGetZepetoHeight(_player.zepetoPlayer));
        });
    }

    *CoGetZepetoHeight(zepeto: ZepetoPlayer) {
        // 使用 WaitForEndOfFrame 准确获取角色的关节位置。
        yield new WaitForEndOfFrame();

        // 获取头部位置。
        const headPosition = zepeto.character.GetSocket(KnowSockets.HEAD_UPPER).position;

        // 获取左脚位置。
        const leftFootPosition = zepeto.character.ZepetoAnimator.GetBoneTransform(HumanBodyBones.LeftFoot).position;

        // 获取右脚位置。
        const rightFootPosition = zepeto.character.ZepetoAnimator.GetBoneTransform(HumanBodyBones.RightFoot).position;

        // 计算两脚之间的中点。
        const characterCenter = Vector3.Lerp(leftFootPosition, rightFootPosition, 0.5);

        // 计算头部与角色中心之间的距离。
        const characterHeight = Vector3.Distance(headPosition, characterCenter);
        console.log(`角色高度从地面: ${characterHeight}`);
    }
}
```



脚本描述

- `*CoGetZepetoHeight(zepeto: ZepetoPlayer)`
  - 使用 `GetSocket` 获取角色的头部插槽位置值。
  - 要获取角色底部中心的位置值，请使用 `GetBoneTransform` 获取角色两脚的位置值。
  - 使用 `Vector3.Lerp` 获取角色底部中心位置值。
  - 使用角色的头部插槽和底部中心位置值获取角色的高度值。



### 步骤 2 : 运行

当你点击播放按钮运行时，可以在控制台日志中看到角色的高度值。

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



:::hint{type="success"}
👍 **提示**

- 示例中测量高度的方法是使用 HeadSocket 的高度值来测量角色的高度。
- 如果您想通过建模角色的头部、帽子和配饰来测量顶部位置，请参考以下链接：
:::

:::hint{type="info"}
**📘 SkinnedMeshRenderer.localBounds**

- [https://docs.unity3d.com/Manual/class-SkinnedMeshRenderer.html](https://docs.unity3d.com/Manual/class-SkinnedMeshRenderer.html)
- [https://docs.unity3d.com/2020.3/Documentation/ScriptReference/SkinnedMeshRenderer-localBounds.html](https://docs.unity3d.com/2020.3/Documentation/ScriptReference/SkinnedMeshRenderer-localBounds.html)
:::

