
[英]Unity: Set RigidBody velocity in FixedUpdate() or Start()?
[英]Unity rigidbody FixedUpdate basic movement does not work
我正在尝试在统一站点上实施本教程。 我已经浏览了统一博客,却没有找到解决我问题的方法。
我在平面上有一个简单的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”,但是脚本正在尝试访问它。 您可能需要向游戏对象“玩家”中添加一个刚体。 或者您的脚本需要在使用组件之前检查组件是否已连接。 UnityEngine.Rigidbody.AddForce(Vector3力)(在C:/buildslave/unity/build/artifacts/generation/common/modules/NewDynamics.gen.cs:706)Controller.FixedUpdate()(在Assets / _Scripts / Controller.cs中:20)
“玩家”是我给球的名字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.