Vector3.Angle()

Hi,

I’m trying to get the angle like below.

But in 3D space not on the same level, I thought this would do it.

function angleToPlayer() : float
{
	targetDir = player.position - transform.position;
	forwardVector = transform.forward;
	angle = Vector3.Angle(forwardVector, targetDir);
	return angle;
}

But unless the enemy is looking down or up, directly at the player the angle is way to high, whats missing?

EDIT … The below angle from the dragon to the green marker on the deck shows 36.62242

41681-screen-shot-00.jpg

The problem (a very annoying one, I might add!) is that Vector3.Angle takes into account ALL rotation axes, not just the y axis. That’s why you get those high angle values unless looking directly at the target. There are basically two workarounds:
a) Make sure both positions are on the same y position
b) Use an object taht only rotates around the y axis: for example, a Character Controller that only rotates left and right, and where the camera is a child of it and only the camera rotates up and down (head rotation).