unity3d - unity:未赋值Movement的变量rb
问题描述
我是 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);
}
}
}
解决方案
推荐阅读
- python - 星像的质心
- laravel - Laravel 5.5 控制器更新功能问题
- python - VS中的tensorflow,已下载但无法正常工作
- javascript - JS 导入不适用于 Prepros 应用程序
- javascript - 由于 MIME 类型,我的 html 上出现错误
- java - 硒黄瓜中运行文件的初始化错误
- python - Python Django 2.0 DecimalField“确保小数点前不超过3位”
- c++ - CMake 在 macOS High Sierra 上找不到 wxWidgets 库
- angular - 如何在 Angular 5 中创建单例服务?
- c++ - C++ 回溯排列