找不到刚体2D组件的Velocity属性
本文关键字:Velocity 属性 组件 2D 找不到 | 更新日期: 2023-09-27 18:29:34
嗨,你知道我不能让我的角色统一移动,因为c#不接受单词"velocity"。请帮我解决这个问题。
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;
}
}
您的问题不是C#不"接受"名为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;
}
}