Unity错误:UnityEngine.Component'不包含'velocity'的定义 [英] Unity Error: UnityEngine.Component' does not contain a definition for `velocity'
问题描述
我对C#还是很陌生,所以如果这很明显,请原谅我.
I am very new to C#, so forgive me if this is obvious.
I am following the steps in this tutorial and have run into a problem on step six. The error it gives is this: The error it gives is this:
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
曾经是从MonoBehaviour
所继承的Component继承的变量.现在已弃用.
rigidbody2D
used to be a variable inherited from Component which MonoBehaviour
is inherits. It is now deprecated.
现在,您必须声明它并使用GetComponent<Rigidbody>();
对其进行初始化,就像对Start()
函数中的Animator(anim
)变量所做的一样.另外,为了不使自己与旧变量混淆,建议您将rigidbody2D
重命名为其他名称.在下面的示例代码中,我将其重命名为rigid2D
并声明它.
Now, you have to declare it and initialize it with GetComponent<Rigidbody>();
just like you did for the Animator(anim
) variable in the Start()
function. Also, to not confuse yourself with the old variable, I suggest that you rename rigidbody2D
to something else. In the example code below, I will rename it to rigid2D
and declare it.
如果不重命名,则可能会显示警告:
If you don't rename it, you might get a warning that says:
严重性代码描述项目文件行抑制状态 警告CS0108'RobotController.rigidbody2D'隐藏继承的成员 'Component.rigidbody2D'.如果打算隐藏,请使用new关键字.
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;
}
}
这篇关于Unity错误:UnityEngine.Component'不包含'velocity'的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!