Normalized Angle Between 2 position vectors

Hi,

I’ve been looking around and all of the answers I found for this subject do not exactly fit my problem.
My project has a top-down 2D view on space ships.

I’m making my own simple AI for an enemy space ship.
Once the player is within a fixed range, the enemy does 3 things:

  1. Rotate towards the player, to face it.
  2. Fly towards the player, or back off, to maintain a certain distance.
  3. Shoot at the player if the enemy is facing it.

Currently I have a problem with the 1st behavior.
Different Ships have different thrusters and so rotate left or right at a different speed.
This is why Transform.LookAt() doesn’t fit here.
I’ve seen an almost perfect solution using PlayerPosition - EnemyPosition vector, then run it’s Y and X components thorugh Atan2.
The problem with this is observed here:
excellentmessyantelope

In the negative X and negative Y quarter of the axis system, there is a gap or a shift between 90 degrees and -270 degrees.
Right when the player crosses to that quarter, this jump from 90 to -270 causes the enemy to rotate in the wrong direction, taking the longer way to face the player.

I’m using this code:

        Vector2 DirectionToPlayer = PlayerShip.transform.position - transform.position;
        DirectionToPlayer.Normalize();

        float AngleToPlayerShip = Mathf.Atan2(DirectionToPlayer.y, DirectionToPlayer.x) * Mathf.Rad2Deg - 90;

        if (NPCBody.rotation - AngleToPlayerShip > 5)
{
ShipToControl.RotateRight();
}
else if..
{
ShipToControl.RotateLeft();
}

Is there a way to get the rotation angle in a consecutive manner throughout all quarters of the axis system?
Or, alternatively, replacing NPCBody.rotation to something else that follows this pattern?

This function is located inside the Update function of the Enemy AI Script.
Note: The -90 in the angle is not the issue, I removed it and the same thing happend but in 180 degrees instead of 90.

Ah yes… you can us this method to truly “difference” your angles:

https://docs.unity3d.com/ScriptReference/Mathf.DeltaAngle.html

Get the delta and it will tell you if you need to go plus or minus rotation-wise.

From that you can decide to turn left or turn right.

Love your little triangles… this is what fun gamedev is all about!

8193123--1068282--Screen Shot 2022-06-09 at 9.45.56 AM.png

It worked like a charm! Thank you so much.

It’s incredible how much time can be saved just by posting something in the forums here.
I usually like to get things done on my own, but stumbling upon the right function to call for this scenario is just so unlikely.

Good thing the community have each other’s backs. :slight_smile:

1 Like

It’s always a pleasure when you take the time to explain your problem as well as you did!

If you get those little AIs working and playing, make a little video and post it over here:

https://forum.unity.com/categories/works-in-progress.673/

Feel free to tag me. Always cool to see alien spacecraft combat!