- Home /
Issue with flipping an enemy when it reaches an edge
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;
}
Comment
Do you want it to flip and move in the other direction? or just flipping?
Best Answer
Answer by Daemonhahn · Nov 06, 2017 at 12:57 PM
why not just flip the spriterenderer and change the direction your moving it in?
https://docs.unity3d.com/ScriptReference/SpriteRenderer-flipX.html
and then save a vector3 for the direction and flip that vector.
Ya that works. Can't believe I hadn't thought of that, thanks!