首页 > 解决方案 > 在 Unity 中使用 Kinect 身体关节位置仅允许 x 位置随时间变化

问题描述

伙计们,我正在制作一个无尽的跑步游戏,它将通过身体位置来控制。

我正在尝试使用 Kinect 传感器将角色向左或向右(x 轴)移动到我的身体位置。角色可以自由地向前移动(z 轴)Time.deltaTime。角色CharacterController附有剧本。移动代码如下:

CharacterController controller;
KinectManager kinectManager;
float speed = 5.0f
Vector3 moveDir;
void Update()
{
    moveDir = Vector3.zero;
    moveDir.z = speed;
    moveDir.x = kinectManager.instance.BodyPosition * speed;

    //controller.Move(moveDir * Time.deltaTime);

    controller.Move(new Vector3 (moveDir.x, 0, moveDir.z * Time.deltaTime));
}

该语句controller.Move(moveDir * Time.deltaTime); 不断向左或向右移动字符,因为 x 位置正在增加,Time.deltaTime所以我想限制它,我将其更改为controller.Move(new Vector3 (moveDir.x, 0, moveDir.z * Time.deltaTime));.

现在发生的事情是角色被卡在同一个位置。我可以随着身体位置向左或向右移动,但不能向前移动。我在这里想念什么?

请帮忙。

标签: c#unity3dkinect

解决方案


识别问题

首先尝试仔细观察您的轴,您的游戏对象 y 轴在哪里,因为您为其分配了 0 值。下面的代码将帮助您找到问题并解决它。

解决方案

void Update()
{
    if (controller.isGrounded)
    {
        // We are grounded, so recalculate
        // move direction directly from axes

        moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
        moveDirection = transform.TransformDirection(moveDirection);
        moveDirection = moveDirection * speed;

        if (Input.GetButton("Jump"))
        {
            moveDirection.y = jumpSpeed;
        }
    }

    // Apply gravity
    moveDirection.y = moveDirection.y - (gravity * Time.deltaTime);

    // Move the controller
    controller.Move(moveDirection * Time.deltaTime);
}

推荐阅读