- Home /
Question by
Oxidize_ · May 23, 2020 at 09:27 AM ·
2d gameenemyenemy ai2d rotation
How to fix enemy's rotation (2D)
So I have a script assigned to my enemy for it to follow my player and the problem is it keeps rotating on the y axis, How can i fix this, Here's the script:
public Transform Player;
int MoveSpeed = 4;
float MaxDist = 10f;
float MinDist = 5f;
void Update ()
{
transform.LookAt(Player);
if (Vector2.Distance(transform.position, Player.position) >= MinDist)
{
transform.position += transform.forward * MoveSpeed * Time.deltaTime;
if (Vector2.Distance(transform.position, Player.position) <= MaxDist)
{
//Here Call a function for shooting
}
}
}
Comment
Best Answer
Answer by frilanski · May 23, 2020 at 09:47 AM
If it has a rigidbody you can just add a constraint so that it cannot rotate in y. The transform should still override this so the script should still work.
Your answer
Follow this Question
Related Questions
make player stuck inside of an enemy ,how to make a player stuck inside of an enemy ? 1 Answer
How can I get my enemy to rotate to look at my character in my 2D game?? 2 Answers
How to flip 2d character walk movement? 3 Answers
I changed my monitor resolution and now my enemies are chasing me too fast ?? 2 Answers