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

本文关键字:运动 不起作用 刚体 fixeduupdate Unity | 更新日期: 2023-09-27 17:49:49

我正在尝试在unity网站上实现这个教程。我已经浏览了unity博客,还没有找到解决我问题的方法。

在平面上有一个简单的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.
}

}

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

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

我错过了什么?

EDIT:如果它是相关的,我也有问题打开Unity c#代码编辑器(忘记它的名字)。每当我点击打开,它就会立即关闭。我用Visual Studio做所有的事情。

EDIT 2:我的错,我刚刚发现我有控制台错误。我得到了下面这个:

MissingComponentException:没有'Rigidbody'附加到"玩家"游戏对象,但脚本试图访问它。你可能需要给游戏对象"Player"添加刚体。或者您的脚本需要在使用组件之前检查该组件是否已附加。UnityEngine.Rigidbody.AddForce (Vector3 force)(在C:/buildslave/unity/build/artifacts/generated/common/modules/NewDynamics.gen.cs:706)控制器。FixedUpdate () (at Assets/_Scripts/Controller.cs:20)

"Player"是我给球体起的名字

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

我忘记把刚体附加到球体上了