Tru*_*ude 3 c# unity-game-engine unity3d-2dtools unity5
我对C#很新,所以如果这很明显,请原谅我.
我正在按照本教程中的步骤操作,并在第六步遇到问题.它给出的错误是:它给出的错误是这样的:
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?'
Run Code Online (Sandbox Code Playgroud)
这是代码:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
错误在第23行:
rigidbody2D.velocity = new Vector3 (move * maxSpeed, rigidbody2D.velocity.y);
Run Code Online (Sandbox Code Playgroud)
rigidbody2D以前是继承自Component的变量MonoBehaviour.它已被弃用.
现在,您必须声明它并初始化它,GetComponent<Rigidbody>();就像您anim对Start()函数中的Animator()变量所做的那样.另外,为了不让自己与旧变量混淆,我建议你重命名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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7925 次 |
| 最近记录: |