Space Shooter 教程中的 RigidBody 自动移动到左上角

RigidBody moving automatically to upper left in Space Shooter tutorial

我正在关注 Unity 网站上的 space 射手教程。

我已经完成了最多玩家对象的移动动作。

当我开始游戏时,space即使没有输入,飞船也会自动移动到左上角。

我完全按照教程的原样进行了操作。即使是 Asset Store 中可用的完整场景也存在同样的问题。

我正在使用 Unity 5.3。

PlayerController.cs

using UnityEngine;
using System.Collections;

[System.Serializable]
public class Boundary
{
    public float xMin, xMax, zMin, zMax;


}


public class PlayerController : MonoBehaviour {


public float speed;
public Boundary boundary;
public float tilt;

// Use this for initialization
void FixedUpdate()
{
    float moveHorizontal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis("Vertical");

    Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);

    GetComponent<Rigidbody>().velocity = movement * speed;

    GetComponent<Rigidbody>().position = new Vector3(
        Mathf.Clamp(GetComponent<Rigidbody>().position.x,boundary.xMin, boundary.xMax),  
        0.0f,
        Mathf.Clamp(GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax));


    GetComponent<Rigidbody>().rotation = Quaternion.Euler(0,0, GetComponent<Rigidbody>().velocity.x * -tilt);
}
}

你的代码似乎是正确的,因为你说演示场景也是如此,我想问题出在你的 Axis 输入上:The lines taht add movement

float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");

使用名为 HorizontalVertical 的轴。在您的统一实例中,这些输入可能链接到发送事件的设备(您可能插入了控制器...)

要对此进行测试,您可以在读取输入的下方添加以下行:

float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Debug.Log("Movement: " + moveHorizontal + ", " + moveVertical); // <-- add this

这将写入您作为输入获得的值。如果你什么都不碰,它们应该为零。如果它们不为零,转到 Edit -> Project Settings -> Input,您将看到您的键盘、鼠标和其他控制器如何链接到 Unity 中的事件,例如 HorizontalVertical

有关输入管理器的详细信息,请参阅http://docs.unity3d.com/Manual/class-InputManager.html

祝你好运!