- Home /
How to determine degrees of one object to another
hey everyone another one thats been bugging me its probably really simple but i cant find a way of working it out so any help would be appreciated.
k now i hope to explain this properly
object A will be moving and rotating constantly, say object A's forward would be 360/0 degrees on the y axis, right would be 90, behind would be 180
now my problem is determining at what degrees object B is to Object A?
probably badly explained so i drew a picture to help lol
Find A and B's positions in world coordinates ptA and ptB.
If you're working in 3 dimensions, you'll need to project ptA and ptB onto a common plane.
The vector from A to B aToB is given by ptB – ptA.
Find the facing of A aFacing by using one of its transform's axes (for example A.transform.forward), optionally rotated however you like.
The angle you're looking for is then just Vector3.Angle( aFacing, aToB ).
Answer by Ed unity · Jan 07, 2015 at 08:44 PM
Using ObjectA.Forward and the vector between ObjectA and ObjectB (B.Position - A.Position) you can find the Angle between using Vector3.Angle()
More information can be found at the below answer page: http://answers.unity3d.com/questions/317648/angle-between-two-vectors.html
Answer by danipren · Jan 07, 2015 at 09:04 PM
thanks for your help ok ive tried this
so basically i have
var ptA = transform.position;
var ptB = referenceCamera.transform.position;
var Diferance = ptB - ptA;
var aFacing = ptA.forward;
FinalAngle = Vector3.Angle(aFacing,Diferance);
now this does work but not properly, im probably doing something wrong but the angle is also changing when the distance between the 2 objects change distance to each other. for examble when the are close and one goes around the other it wont go below 20 but if i go far out it will go below zero .
what im trying to achieve is that when object b is between (for example) 90 degrees and 180 degrees do something, 180 degrees to 270 do something else , 270 to 360 etc etc
thanks again and much appreciated
I'm also facing a similar problem, did you ever figure out how to make it work at any distance?
Your answer
Follow this Question
Related Questions
How to do spherecast? 1 Answer
Why do we use the "transform." in hit.transform.gameObject.tag? 1 Answer
What are these things doing in C#? 3 Answers
how to use EditorGUI.IntField 1 Answer
Determine global movement of gameObject 2 Answers