Unity刚体FixedUpdate基本运动不起作用

时间:2015-05-20 08:38:48

标签: c# unity3d

我正在尝试在统一网站上实施this教程。我已经浏览了团结博客,并没有在那里找到我的问题的解决方案。

我在一架飞机上有一个简单的Rigidbody球体物体。 球体是默认大小,并设置为:(0,0.5,0)。 该平面也是默认大小,并在原点(0,0,0)上设置。这些是我使用的唯一组件。 我要做的是为球体编写一个简单的C#脚本行为,将其移动到平面上,如下所示:

public class Controller : MonoBehaviour {

private Rigidbody rb; // Holds the body this script is affecting.

// Called at the start, to set variables.
void Start()
{
    rb = GetComponent<Rigidbody>(); // Get the body, if there is one.
}

//For physical changes.
void FixedUpdate()
{
    float Horizontal = Input.GetAxis ("Horizontal"); // Get horizontal movement from input.
    float Vertical = Input.GetAxis ("Vertical"); // Get vertical movement from input.
    Vector3 Movement = new Vector3 (Horizontal, 0.0f, Vertical); // Declaring the movement I'd like to add to the RB. Y axis is irrelevant. X,Z - controlled by user input.
    rb.AddForce (Movement); // Making the movement.
}

}

我将此行为附加到球体上,期望当我按下某个输入键时它会移动。

尽管如此,当我玩这个项目时,一切都编译得相当好,但无论我输入什么,球体都不会移动。

我错过了什么?

编辑:如果相关,我也无法打开Unity c#代码编辑器(忘了它的名字)。无论何时我点击打开,它都会立即关闭。我在Visual Studio上做了一切。

编辑2 :我的不好,我发现我有控制台错误。我得到以下一个:

  

MissingComponentException:“Player”游戏对象没有附加“Rigidbody”,但是脚本正在尝试访问它。   您可能需要将Rigidbody添加到游戏对象“Player”中。或者您的脚本需要在使用之前检查组件是否已连接。   UnityEngine.Rigidbody.AddForce(Vector3 force)(在C:/buildslave/unity/build/artifacts/generated/common/modules/NewDynamics.gen.cs:706)   Controller.FixedUpdate()(在Assets / _Scripts / Controller.cs:20)

“玩家”是我给球体命名的名称。

1 个答案:

答案 0 :(得分:1)

相关问题