- Home /
Vertical Angle Between two game objects
The blue angle in the attached photo is what I need. You can see I have a direction (drawn in red in unity) that is correct. The start of the direction is a camera on the AI NPC (on the right). And the end is the player.
Vector3 dir = targetHead.transform.position - camera.Value.transform.position;
Debug.DrawRay(camera.Value.transform.position, dir, Color.red);
I'm trying to get the angle because I need to lerp the npc object mecanim body_vertical property (-1 to 1) relative to that angle so that the npc is always aiming at the player.
I have tried numerous solutions, they all end up with an angle that is projected on the plane the npc is standing on instead of a -90 (looking down) all the way up to 90 (looking up) regardless of which direction the source or the target is facing x and z. Any help would be great.
Looking at the image, you can see you only need to draw 1 more line to complete the right triangle you have started. If you know trigonometry, it's pretty easy to calculate any angles from there.
I presume you know the point (vector A) at which the blue and red lines meet (character's head). You probably also know (vector B) where the object to look at is. Just take vector B and replace the y-component of it with the y-component of A and you have the 3rd corner of your right triangle.
I knew it was a trig thing and that was my next step. I was hoping for something elegant like what elenzil suggested using products.
Answer by JSierraAKAMC · Feb 16, 2017 at 06:04 PM
This seems like a trigonometry question, like NoseKills said. Assuming you have GameObjects 'targetHead' and 'camera' (that's what they appear to be named in the code snippet), the vertical angle could be found with this code:
///You know the vertical difference (we'll call it side b of the triangle) of the transforms and the horizontal difference (we'll call it side a of the triangle). Therefore the tangent of angle B (the corner opposite side b) would be equal to (side b / side a). To find the angle in radians, you would find the arctangent of that value and then convert it to an angle.
float sideA = Vector3.Distance(camera.transform.position, new Vector3(targetHead.transform.position.x, camera.transform.position.y, targetHead.transform.position.z));
//Horizontal distance to targetHead
float sideB = targetHead.transform.position.y - camera.transform.position.y;
//Vertical difference
float angleRad = Mathf.Atan(sideB/sideA);
//Or if degrees are needed
float angleDeg = angleRad * Mathf.Rad2Deg;
Fantastic! In that photo angleDeg
is 25.5 and when on the ground with the NPC its -0.6. That's what I needed.
Answer by elenzil · Feb 16, 2017 at 06:58 PM
NoseKills and JSierraAKAMC are on the right track to use trig, but i think there's a better approach.
it sounds like you want to find the angle between two vectors. this is pretty straight-forward.
the dot-product of two vectors is the cosine of the angle between the vectors times the length of each vector. so you can go in reverse. first, make normalized copies of the vectors (to make their length equal to one), then take the dot-product of the normalized vectors, and then the ArcCosine of that.
float radiansBetweenVectors(Vector3 vA, Vector3 vB) {
vA.normalize();
vB.normalize();
float ADotB = vA.Dot(vB);
float radians = Math.Acos(ADotB);
return radians;
}
Thanks for giving that a shot, unfortunately, the value is 6.2 in that photo and 3.3 when on the ground and 0.2 when right in front of the NPC. Also, to get your code to work the following was required, hopefully I didn't change the context.
float degreesBetweenVectors(Vector3 vA, Vector3 vB) {
vA.Normalize();
vB.Normalize();
float ADotB = Vector3.Dot(vA, vB);
float radians = $$anonymous$$athf.Acos(ADotB);
return radians * $$anonymous$$athf.Rad2Deg;
}
Your answer
Follow this Question
Related Questions
Need a little help with quick angle equation 2 Answers
Getting angle where viewport intersects trigger 0 Answers
Calc 30° left and x units forward from local forward axis 1 Answer
Transform.RotateAround(Vector3 axis, float angle) - what happens with only two arguments? 3 Answers
Why am I not getting an angle in degrees from aTan? 1 Answer