Unity3d CharacterController.move奇怪的行为
您好,我有一个关于charactercontroller的问题。当我使用以下代码向左、向右、向前和向后旋转字符时,移动函数可以正常工作Unity3d CharacterController.move奇怪的行为,unity3d,accelerometer,unityscript,Unity3d,Accelerometer,Unityscript,您好,我有一个关于charactercontroller的问题。当我使用以下代码向左、向右、向前和向后旋转字符时,移动函数可以正常工作 function Update () { var controller : CharacterController = GetComponent(CharacterController); if (controller.isGrounded) { // We are grounded, so recalculate /
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值仍在不断变化和增加。有人能解释原因吗?可能需要根据设备旋转重新映射输入。加速输入的帮助页面显示了如何将传入数据重新映射到游戏环境。谢谢回复,但这不是因为重新映射,输入映射就像我向右倾斜设备一样,它向右移动,反之亦然。我的播放器保持向右移动,即使我保持设备静止。我也尝试了上面的代码。它有一个相同的问题,即来自加速计的值会改变播放器的x和z位置,即使我让设备保持静止。在使用加速计进行任何计算之前,您能否打印出加速计的原始值,以查看它们是否与您的输入相符。如果他们这样做了,那么至少你知道问题在于计算。