Unity 错误:UnityEngine.Component'不包含 `velocity' 的定义; [英] Unity Error: UnityEngine.Component' does not contain a definition for `velocity'

查看:57
本文介绍了Unity 错误:UnityEngine.Component'不包含 `velocity' 的定义;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 C# 很陌生,所以如果这很明显,请原谅我.

I am very new to C#, so forgive me if this is obvious.

我正在按照 本教程 并且在第 6 步中遇到了问题.它给出的错误是这样的:它给出的错误是这样的:

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(); 对其进行初始化,就像您对 中的 Animator(anim) 变量所做的一样>Start() 函数.另外,为了不要与旧变量混淆,我建议您将 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&amp;#39;不包含 `velocity&amp;#39; 的定义;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆