Unity Error: UnityEngine.Component' does not contain a definition for `velocity'
Unity Error: UnityEngine.Component' does not contain a definition for `velocity'
我是 C# 的新手,如果这很明显,请原谅我。
我正在按照 this tutorial 中的步骤进行操作,并且 运行 在第六步遇到了问题。它给出的错误是这样的:它给出的错误是这样的:
UnityEngine.Component' does not contain a definition for `velocity' and no extension method `velocity' of type `UnityEngine.Component' could be found. Are you missing an assembly reference?'
代码如下:
using UnityEngine;
using System.Collections;
public class RobotController : MonoBehaviour {
//This will be our maximum speed as we will always be multiplying by 1
public float maxSpeed = 2f;
//a boolean value to represent whether we are facing left or not
bool facingLeft = true;
//a value to represent our Animator
Animator anim;
// Use this for initialization
void Start () {
//set anim to our animator
anim = GetComponent<Animator>();
}
// Update is called once per frame
void FixedUpdate () {
float move = Input.GetAxis ("Horizontal");//Gives us of one if we are moving via the arrow keys
//move our Players rigidbody
rigidbody2D.velocity = new Vector3 (move * maxSpeed, rigidbody2D.velocity.y);
//set our speed
anim.SetFloat ("Speed",Mathf.Abs (move));
//if we are moving left but not facing left flip, and vice versa
if (move < 0 && !facingLeft) {
Flip ();
} else if (move > 0 && facingLeft) {
Flip ();
}
}
//flip if needed
void Flip(){
facingLeft = !facingLeft;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
错误在第 23 行:
rigidbody2D.velocity = new Vector3 (move * maxSpeed, rigidbody2D.velocity.y);
rigidbody2D
曾经是从 Component 继承的变量,而 MonoBehaviour
是继承的。现在已弃用。
现在,您必须声明它并用 GetComponent<Rigidbody>();
初始化它,就像您在 Start()
函数中对 Animator(anim
) 变量所做的那样。另外,为了不与旧变量混淆,我建议您将 rigidbody2D
重命名为其他名称。在下面的示例代码中,我将其重命名为 rigid2D
并声明它。
如果您不重命名它,您可能会收到一条警告:
Severity Code Description Project File Line Suppression State
Warning CS0108 'RobotController.rigidbody2D' hides inherited member
'Component.rigidbody2D'. Use the new keyword if hiding was intended.
public class RobotController: MonoBehaviour
{
public float maxSpeed = 2f;
//a boolean value to represent whether we are facing left or not
bool facingLeft = true;
//a value to represent our Animator
Animator anim;
//Declare rigid2D
Rigidbody rigid2D;
// Use this for initialization
void Start()
{
//set anim to our animator
anim = GetComponent<Animator>();
//Initialize rigid2D
rigid2D = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate()
{
float move = Input.GetAxis("Horizontal");//Gives us of one if we are moving via the arrow keys
//move our Players rigidbody
rigid2D.velocity = new Vector3(move * maxSpeed, rigid2D.velocity.y);
//set our speed
anim.SetFloat("Speed", Mathf.Abs(move));
//if we are moving left but not facing left flip, and vice versa
if (move < 0 && !facingLeft)
{
Flip();
}
else if (move > 0 && facingLeft)
{
Flip();
}
}
//flip if needed
void Flip()
{
facingLeft = !facingLeft;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
你只是在start函数中创建了一个刚体对象,
Rigidbody rigidbody = GetComponent<Rigidbody>();
如果您使用的是 2D 动画,请使用以下代码,
Rigidbody2D rigidbody = GetComponent<Rigidbody2D>();
我是 C# 的新手,如果这很明显,请原谅我。
我正在按照 this tutorial 中的步骤进行操作,并且 运行 在第六步遇到了问题。它给出的错误是这样的:它给出的错误是这样的:
UnityEngine.Component' does not contain a definition for `velocity' and no extension method `velocity' of type `UnityEngine.Component' could be found. Are you missing an assembly reference?'
代码如下:
using UnityEngine;
using System.Collections;
public class RobotController : MonoBehaviour {
//This will be our maximum speed as we will always be multiplying by 1
public float maxSpeed = 2f;
//a boolean value to represent whether we are facing left or not
bool facingLeft = true;
//a value to represent our Animator
Animator anim;
// Use this for initialization
void Start () {
//set anim to our animator
anim = GetComponent<Animator>();
}
// Update is called once per frame
void FixedUpdate () {
float move = Input.GetAxis ("Horizontal");//Gives us of one if we are moving via the arrow keys
//move our Players rigidbody
rigidbody2D.velocity = new Vector3 (move * maxSpeed, rigidbody2D.velocity.y);
//set our speed
anim.SetFloat ("Speed",Mathf.Abs (move));
//if we are moving left but not facing left flip, and vice versa
if (move < 0 && !facingLeft) {
Flip ();
} else if (move > 0 && facingLeft) {
Flip ();
}
}
//flip if needed
void Flip(){
facingLeft = !facingLeft;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
错误在第 23 行:
rigidbody2D.velocity = new Vector3 (move * maxSpeed, rigidbody2D.velocity.y);
rigidbody2D
曾经是从 Component 继承的变量,而 MonoBehaviour
是继承的。现在已弃用。
现在,您必须声明它并用 GetComponent<Rigidbody>();
初始化它,就像您在 Start()
函数中对 Animator(anim
) 变量所做的那样。另外,为了不与旧变量混淆,我建议您将 rigidbody2D
重命名为其他名称。在下面的示例代码中,我将其重命名为 rigid2D
并声明它。
如果您不重命名它,您可能会收到一条警告:
Severity Code Description Project File Line Suppression State Warning CS0108 'RobotController.rigidbody2D' hides inherited member 'Component.rigidbody2D'. Use the new keyword if hiding was intended.
public class RobotController: MonoBehaviour
{
public float maxSpeed = 2f;
//a boolean value to represent whether we are facing left or not
bool facingLeft = true;
//a value to represent our Animator
Animator anim;
//Declare rigid2D
Rigidbody rigid2D;
// Use this for initialization
void Start()
{
//set anim to our animator
anim = GetComponent<Animator>();
//Initialize rigid2D
rigid2D = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate()
{
float move = Input.GetAxis("Horizontal");//Gives us of one if we are moving via the arrow keys
//move our Players rigidbody
rigid2D.velocity = new Vector3(move * maxSpeed, rigid2D.velocity.y);
//set our speed
anim.SetFloat("Speed", Mathf.Abs(move));
//if we are moving left but not facing left flip, and vice versa
if (move < 0 && !facingLeft)
{
Flip();
}
else if (move > 0 && facingLeft)
{
Flip();
}
}
//flip if needed
void Flip()
{
facingLeft = !facingLeft;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
你只是在start函数中创建了一个刚体对象,
Rigidbody rigidbody = GetComponent<Rigidbody>();
如果您使用的是 2D 动画,请使用以下代码,
Rigidbody2D rigidbody = GetComponent<Rigidbody2D>();