2D game sprite rotates on X and Y axis instead of Z axis only

I’m making a 2d game and I have the orientation set as YAxisForward on the AIPath script. The AIPath script is being used to control the movement of the character but I have a separate script to ensure the character is facing it’s target

transform.up = (enemyTargetFinderControllerScript.closestTarget.position - transform.position) * -1;

However, sometimes the AI character starts rotating around it’s X and Y axis too. Any idea what could be causing this?

Thanks!


image

Hi

If you are rotating the agent yourself you probably want to set ai.updateRotation = false to disable the agent’s own rotation.

In this case I think the X and Y rotation comes from the fact that the target and the agent have different Z coordinates. You could instead do something like this:

var dir = -(enemyTargetFinderControllerScript.closestTarget.position - transform.position);
dir.z = 0;
transform.up = dir;
1 Like

You are my hero. After reading you mention the possibility of the Z position changing, that’s exactly what the issue was. I can’t thank you enough!

1 Like