【发布时间】:2018-11-20 00:17:46
【问题描述】:
我要做的就是当敌人转身时,它的精灵会翻转,下面的代码是我所管理的,但现在它只在两次接近但不是我需要的方向改变后翻转。请帮忙。
[HideInInspector]
public bool facingRight = true;
public float speed;
Rigidbody2D rbody;
public float timer;
Animator anim;
public float directionx;
// Use this for initialization
void Start()
{
rbody = GetComponent<Rigidbody2D>();
StartCoroutine(Move(timer));
}
IEnumerator Move(float timer)
{
while (0 < 1)
{
Vector2 targetVelocity = new Vector2(directionx, 0);
rbody.velocity = targetVelocity * speed;
yield return new WaitForSeconds(timer);
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
Vector2 targetVelocity1 = new Vector2(-directionx, 0);
rbody.velocity = targetVelocity1 * speed;
yield return new WaitForSeconds(timer);
}
}
}
【问题讨论】:
-
我知道你已经接受了一个答案,但是如果你使用
SpriteRenderer来渲染你的精灵,你为什么不直接使用flipX 属性,这不应该解决问题吗? SpriteRenderer.flipX