嗨,你知道我不能让我的角色团结一致,因为c#不接受“速度”这个词。请帮我解决这个问题。
public class MarioController : MonoBehaviour
{
public float maxSpeed=10f;
bool facingRight=true;
void Start ()
{}
void FixedUpdate ()
{
float move = Input.GetAxis ("Horizontal");
rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
if (move > 0 && !facingRight)
Flip ();
else if (move < 0 && facingRight)
Flip ();
}
void Flip ()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
答案 0 :(得分:3)
你的问题不,C#没有&#34;接受&#34;名为Velocity的属性,你的问题是你从未声明过rigibody2D变量。通常情况下,你会这样:
如果您正在使用Unity3D 5.3,那么您可以使用它来翻转精灵,它应该有更好的性能:
public class MarioController : MonoBehaviour
{
public float maxSpeed=10f;
bool facingRight=true;
Rigibody2D rigibody2D;
SpriteRenderer spriteRenderer;
void Start()
{
rigibody2D = GetComponent<Rigibody2D>(); //get the reference to the Rigibody2D component of this GameObject
spriteRenderer = GetComponent<SpriteRenderer>(); //get the reference to the SpriteRenderer component of this GameObject
}
void FixedUpdate ()
{
float move = Input.GetAxis ("Horizontal");
rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
if (move > 0 && !facingRight)
Flip (facingRight);
else if (move < 0 && facingRight)
Flip (facingRight);
}
void Flip (bool flip)
{
facingRight = !flip;
spriteRenderer.flipX = flip;
//facingRight = !facingRight;
//Vector3 theScale = transform.localScale;
//theScale.x *= -1;
//transform.localScale = theScale;
}
}