【问题标题】:Unity Error: UnityEngine.Component' does not contain a definition for `velocity'Unity 错误:UnityEngine.Component' 不包含“速度”的定义
【发布时间】:2017-01-15 03:12:13
【问题描述】:

我对 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);

【问题讨论】:

    标签: c# unity3d unity5 unity3d-2dtools


    【解决方案1】:

    rigidbody2D 曾经是一个继承自 MonoBehaviour 的 Component 的变量。现在已弃用。

    现在,您必须声明它并使用 GetComponent&lt;Rigidbody&gt;(); 对其进行初始化,就像您为 Start() 函数中的 Animator(anim) 变量所做的那样。此外,为了不将自己与旧变量混淆,我建议您将 rigidbody2D 重命名为其他名称。在下面的示例代码中,我将它重命名为rigid2D 并声明它。

    如果您不重命名它,您可能会收到一条警告:

    严重性代码描述项目文件行抑制状态 警告 CS0108 'RobotController.rigidbody2D' 隐藏继承的成员 'Component.rigidbody2D'。如果要隐藏,请使用 new 关键字。

    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;
        }
    }
    

    【讨论】:

    • 除了省略 using UnityEngine;using System.Collections; 之外,效果很好。
    • 我故意将它们排除在外,因为您的问题中已经包含它们。您所要做的就是用新班级替换班级中的所有内容。
    • @Programmer 为此浪费了半个小时,感谢您的回答
    【解决方案2】:

    你只需在开始函数中创建一个刚体对象,

    Rigidbody rigidbody = GetComponent<Rigidbody>();
    

    如果您使用的是 2D 动画,请使用以下代码,

    Rigidbody2D rigidbody = GetComponent<Rigidbody2D>();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-10
      • 2018-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多