How can I get the rotation between two objects


I want to do something like this:

  • I have a Player;
  • I have a enemy;
  • The enemy smooth look at the player

How can I get the rotation of the enemy based on the player position?

In others words, I want to get 0 angle when the enemy is exactly facing to the player.

I try this but doen't works as I expect:

var rot : Quaternion;
print(rot.y - transform.rotation.y);

I got it works making this:

I created an empty object named lookTo and add as child of enemy.

Use this script:

var a = (Mathf.Sign(lookTo.rotation.y)*lookTo.rotation.y) - (Mathf.Sign(transform.rotation.y)*transform.rotation.y);
print( a*Mathf.Sign(a) );