Unity:运动的变量rb尚未被赋值。

4

我是Unity的新手,正在按照这个教程进行学习。当我尝试执行移动玩家左右的代码时,我遇到了以下错误:

UnassignedReferenceException: The variable rb of Movement has not been assigned.
You probably need to assign the rb variable of the Movement script in the inspector.
UnityEngine.Rigidbody.AddForce (UnityEngine.Vector3 force, UnityEngine.ForceMode mode) <0x3cc39790 + 0x00062> in <31353b3f2d2e434595cbfe07fbec4072>:0
UnityEngine.Rigidbody.AddForce (System.Single x, System.Single y, System.Single z) (at C:/buildslave/unity/build/Runtime/Dynamics/ScriptBindings/Dynamics.bindings.cs:176)
Movement.FixedUpdate () (at Assets/Movement.cs:17)

如何修复因玩家无法移动而导致的问题?

移动脚本:
   using UnityEngine;

public class Movement : MonoBehaviour
{

    // This is a reference to the Rigidbody component called "rb"
    public Rigidbody rb;
    public float forwardForce = 2000f;  // Variable that determines the forward force
    public float sidewaysForce = 500f;  // Variable that determines the sideways force

    // We marked this as "Fixed"Update because we
    // are using it to mess with physics.
    void FixedUpdate()
    {
        // Add a forward force
        rb.AddForce(0, 0, forwardForce * Time.deltaTime);

        if (Input.GetKey("d"))  // If the player is pressing the "d" key
        {
            // Add a force to the right
            rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0);
        }

        if (Input.GetKey("a"))  // If the player is pressing the "a" key
        {
            // Add a force to the left
            rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0);
        }
    }
}
1个回答

9

您还没有设置任何内容到rb。在检查器中,您可以通过点击"this"(参见图片)来选择要使用的刚体。

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接