- Home /
Turn the sprite
Hi, can you help me? I need a way to turn the enemy around. The enemy must walk towards the player and obviously must be turned towards him, on all sides. is a 2D game. I have tried with this code, but it is very slow
void flipAnimazion(){
if(player.position.x >= 0 || player.position.y >= 0){
animazion.SetBool("run", true);
myTransform.localScale = new Vector3(player.position.x +
player.position.y < 0 ? 0.1487581f : 0.1487581f, 0.1544615f,
0.155289f);
} else if(player.position.x < 0 || player.position.y < 0 ) {
animazion.SetBool("run", true);
myTransform.localScale = new Vector3(player.position.x +
player.position.y > 0 ? 0.1487581f : -0.1487581f, 0.1544615f,
0.155289f);
} else {
animazion.SetBool("run", false);
}
}
Answer by NikkF · Mar 14 at 12:42 PM
If you need them rotating fully (360 degrees), you should be able to simply use:
transform.right = target.position - transform.position;
And if you need them to look left and right (flip) you can check if the velocity is above or below x and then flip the sprite using:
spriteRenderer.flipX = true;
Hope this helps!
Your answer
Follow this Question
Related Questions
Left Click While Holding Right Click 1 Answer
How to make jump NO FLY in 2d ? 1 Answer
Cannot seem to randomly generate my colors on my images 1 Answer
How to get pivot of sprite in script 2 Answers
Help me understand SyncVars please 0 Answers