【发布时间】: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