使用Unity中的Kinect Body Joint Position,仅允许x Position随时间变化

问题描述 投票:0回答:1

伙计我正在进行一场无尽的赛跑比赛,这场比赛将由身体姿势控制。

我正在尝试使用Kinect传感器向左或向右移动字符(x轴)和我的身体位置。角色可以使用Time.deltaTime自由前进(z轴)。该角色附有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# unity3d kinect
1个回答
0
投票

Indentifying issues

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

Solution

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);
}
© www.soinside.com 2019 - 2024. All rights reserved.