【问题标题】:Unity rigidbody FixedUpdate basic movement does not workunity 刚体 FixedUpdate 基本运动不起作用
【发布时间】:2015-08-01 09:25:59
【问题描述】:

我正在尝试在统一站点上实施 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:“玩家”游戏对象没有附加“刚体”,但脚本正在尝试访问它。 您可能需要将刚体添加到游戏对象“玩家”。或者您的脚本需要在使用之前检查组件是否已附加。 UnityEngine.Rigidbody.AddForce (Vector3 force) (在 C:/buildslave/unity/build/artifacts/generated/common/modules/NewDynamics.gen.cs:706) Controller.FixedUpdate() (在 Assets/_Scripts/Controller.cs:20)

“玩家”是我给球体起的名字。

【问题讨论】:

  • 这个错误看起来很准确地说明了问题所在,你现在解决了吗?
  • @AdamH 是的,不错的名字顺便说一句。 (:

标签: c# unity3d


【解决方案1】:
猜你喜欢
  • 2021-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多