- Home /
Enemy Keeps flipping on X axis
I'm trying to have the enemy follow the player and flipping the sprite to keep the enemy facing the player. However, when the player is on top of the enemy, they're neither on the left nor right of the enemy so the enemy starts flipping erratically. Here's my code trying to fix the issue, but it doesn't work:
private void ChasePlayer()
{
// left side of player
if (transform.position.x < player.position.x)
{
rb.velocity = new Vector2(moveSpeed, rb.velocity.y);
transform.localScale = Vector3.one;
isFacingLeft = false;
}
else if (transform.position.x > player.position.x) //right side of player
{
rb.velocity = new Vector2(-moveSpeed, rb.velocity.y);
transform.localScale = new Vector3(-1f, 1f, 1f);
isFacingLeft = true;
} else
{
transform.localScale = Vector3.one;
}
}
Any help would be appreciated, thank you in advance.
Answer by landings · Apr 25 at 03:42 PM
You can limit their response frequency.
private float startTime = 0;
private void ChasePlayerWithLimitedFrequency()
{
if (Time.time - startTime < 1f) // 1 second limit
return;
startTime = Time.time;
// left side of player
if (transform.position.x < player.position.x)
{
rb.velocity = new Vector2(moveSpeed, rb.velocity.y);
transform.localScale = Vector3.one;
isFacingLeft = false;
}
else if (transform.position.x > player.position.x) //right side of player
{
rb.velocity = new Vector2(-moveSpeed, rb.velocity.y);
transform.localScale = new Vector3(-1f, 1f, 1f);
isFacingLeft = true;
}
else
{
transform.localScale = Vector3.one;
}
}
I'm still relatively new to Unity, could use explain how your if statement works a little more in detail please?
startTime
records the starting time of every period. Time.time
gives you the current time. Time.time - startTime < 1f
means, if current time is within 1 second delay of your recorded start time, this function is not allowed to run. Every time it is allowed to run, it will prepare for the next period by setting a new start time startTime = Time.time
. private float startTime = 0;
determines if this function is allowed to run in the first few frames. 0 value means it may not allowed to run in the first few frames, great negative value such as -100 means it is allowed to run.
So we're subtracting the current time from the time when the method was fired? And then checking if the difference equals the time we set for the delay?
Note: You probably should remove the final else case. It doesn't make much sense the way it is. First of all the chance that this case is even hit is extremely small because the chance that the two x positions are exactly equal is very small. However if it is hit it means you're just flipping the enemy to the right but you don't change the velocity or your "facing" boolean. So it creates an inconsistency for absolutely no reason. The best fix is to remove the final else block and just make one of the two conditions <=
or >=
so it also covers that rare case. Though since this wouldn't happen very often (especially when you now restrict the response time) just removing the else would do it.
Your answer
![](https://koobas.hobune.stream/wayback/20220613055816im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Image swapping sprites delay. 0 Answers
Tilemap disappearing from game-view after using SetActive 1 Answer
Why are my pixel art black in game window but not in scene windows? 2 Answers
How to crop sprite during runtime? 2 Answers
Changing the orthographicSize of the camera to make sure play field fits 0 Answers