# How would I properly flip a character when they reach the edge of a ground object in Unity 2D?

The idea here is that the raycast shoots downwards from an empty game object and when it detects that there is no ground below it the character should bounce back and walk the other way. When I get to the edge of my platform the character just gets stuck in place and keeps rotating on the Y axis.

public class npcAI : MonoBehaviour
{
public float speed;
public float distance;
private bool movingRight = true;
public Transform groundDetection; //raycast goes downward from this point

private Vector2 movementDirection;

void Start()
{
//Initialise direction
movementDirection = Vector2.right;
}

void Update()
{
transform.Translate(movementDirection * speed * Time.deltaTime);
RaycastHit2D groundInfo = Physics2D.Raycast(groundDetection.position, Vector2.down, distance);
if (groundInfo.collider == false)
{
if (movingRight)
{
movingRight = false;
movementDirection = Vector2.left;
transform.localScale -= new Vector3(0.30F, 0, 0);

}
else
{
movingRight = true;
movementDirection = Vector2.right;
transform.localScale += new Vector3(0.30F, 0, 0);
}

}
}


The code above was edited to reflect the answer below. If you find yourself attempting to do the same thing and need to flip your sprite when you reach the edge change 0.30F to whatever double the X value of your sprite is in order to flip it. My sprites scale was (0.15, 0.15, 0) so I used 0.30F to flip my sprite. Be mindful of when you should use -= and when you should use +=.

• Your raycast should not be perpendicular to the ground but rather oblique. to avoid being stuck at that frame where the raycast will always return false. – Omar Guendeli Nov 7 '18 at 9:59

It looks as though your if block is wrong:

if (groundInfo.collider == false)
{
if (movingRight == true)
{
transform.eulerAngles = new Vector3(0, -180, 0);
}
movingRight = false;
}
else
{
transform.eulerAngles = new Vector3(0, 0, 0);
movingRight = true;
}


if (groundInfo.collider == false)
{
if (movingRight)
{
transform.eulerAngles = new Vector3(0, -180, 0);
movingRight = false;
}
else
{
transform.eulerAngles = new Vector3(0, 0, 0);
movingRight = true;
}
}


Personally I would have done something like this:

public class npcAI : MonoBehaviour
{
public float speed;
public float distance;
private bool movingRight = true;
public Transform groundDetection; //raycast goes downward from this point

private Vector2 movementDirection;

void Start()
{
//Initialise direction
movementDirection = Vector2.right;
}

void Update()
{
transform.Translate(movementDirection * speed * Time.deltaTime);
RaycastHit2D groundInfo = Physics2D.Raycast(groundDetection.position, Vector2.down, distance);
if (groundInfo.collider == false)
{
if (movingRight)
{
movingRight = false;
movementDirection = Vector2.left;

}
else
{
movingRight = true;
movementDirection = Vector2.right;
}

}
}
}

• You should format the original code that you corrected to make it easier to see where the issue is. It took me several passes to realize flag was out of scope from the check. – Stephan Nov 7 '18 at 15:24
• This was really helpful and I added a couple lines in order to flip the sprite itself. – Mark Gregg Nov 7 '18 at 22:24
• I think I'm going to add it here in case anyone comes across this question later and needs help. – Mark Gregg Nov 7 '18 at 22:32
• @Stephan I have updated the formatting as recommended. – King-Xero Nov 9 '18 at 9:45