I'm using a linecast to detect whenever an enemy reaches an edge and then change its direction. When the enemy reaches an edge, it stops as it is supposed to, but it doesn't flip.
Here is the relevant code:
void Update () {
transform.Translate (new Vector2 (_transform.localScale.x, 0) * moveSpeed * Time.smoothDeltaTime * stop);
// enemy turns around when gets to an edge
if (!Physics2D.Linecast (_transform.position, groundCheck.position, whatIsGround) && stop == 1)
StartCoroutine (Flip ());
}
IEnumerator Flip(){
stop = 0;
yield return new WaitForSecondsRealtime (1);
Vector3 localScale = _transform.localScale;
localScale.x *= -1;
_transform.localScale = localScale;
stop = 1;
}
↧