1

嗨,当我使用以下代码向左、向右、向前和向后转动字符时,我有一个关于 charactercontroller.Move() 函数的问题,它工作正常

function Update () {
  var controller : CharacterController = GetComponent(CharacterController);
    if (controller.isGrounded) {
        // We are grounded, so recalculate
        // move direction directly from axes
         accel = Vector3.Lerp(accel, Input.acceleration, 5.0f* Time.deltaTime);
        moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
                                Input.GetAxis("Vertical"));
        moveDirection = transform.TransformDirection(moveDirection);
        moveDirection *= speed;

        if (Input.GetButton ("Jump")) {
            moveDirection.y = jumpSpeed;
        }
    }
    // Apply gravity
    moveDirection.y -= gravity * Time.deltaTime;

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

例如,当我将它从当前位置向左移动到 0.35f 时,它会移动到 0.35f 并停在那里。但是当我尝试通过加速度计移动播放器时,它的工作方式不同我只是在上面的代码中修改一行对此:

function Update () {

  var controller : CharacterController = GetComponent(CharacterController);
    if (controller.isGrounded) {
        // We are grounded, so recalculate
        // move direction directly from axes
         accel = Vector3.Lerp(accel, Input.acceleration, 5.0f* Time.deltaTime);
         inputX=accel.x;
        print(inputX);
        moveDirection = Vector3(inputX, 0,
                                Input.GetAxis("Vertical"));
        moveDirection = transform.TransformDirection(moveDirection);
        moveDirection *= speed;

        if (Input.GetButton ("Jump")) {
            moveDirection.y = jumpSpeed;
        }
    }
    // Apply gravity
    moveDirection.y -= gravity * Time.deltaTime;

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

我还注意到,当我不移动设备时,inputX 的日志几乎保持不变,但控制器的 x 值仍然不断变化和增加。谁能解释原因是什么?

4

1 回答 1

0

也许您需要根据设备旋转重新映射输入。加速输入的帮助页面 ( http://docs.unity3d.com/Documentation/ScriptReference/Input-acceleration.html ) 显示了如何将传入数据重新映射到您的游戏环境。

于 2013-07-08T15:24:19.170 回答