- Home /
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
Answer by Cherno · Mar 02, 2015 at 09:35 PM
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).
Your answer
![](https://koobas.hobune.stream/wayback/20220613184815im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Vector3.Angle returning wrong values for vectors with small components 2 Answers
problem with vector3.Angle with a fixed joint 2 Answers
2D angle of two Vec3 0 Answers
My Camera Do Not Limit Its Angles And It Glitches, How I Can Solution It? 0 Answers
How do I fire a 2D projectile based on it's current angle/location. 1 Answer