CharacterController.move奇怪的行为

时间:2013-07-08 14:59:56

标签: unity3d accelerometer unityscript

我有一个关于charactercontroller.Move()函数的问题当我使用下面的代码来转换字符left,right,forward&回来它工作正常

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值仍在不断变化和增加。任何人都可以解释原因是什么?

1 个答案:

答案 0 :(得分:0)

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