- Home /
How to calculate direction between 2 objects
Hi guys, I'm trying to figure out how to calculate the direction between 2 objects. I already got this.
Debug.DrawLine (camera.transform.position, this.transform.position, Color.red, Mathf.Infinity);
With this code i get a DrawLine between object 1 (camera) and object 2 (my object) But i need the line to go further then just object 2, so I need to know the direction/angle or something.
So my question is: How to calculate the direction/angle or something between these objects.
Hi,
You need 3 points/objects to calculate an angle or 2 vectors. With this code you don't have enough information to get an angle, you need a referential item...
I hope it is clear :p
regards
Answer by Berenger · Apr 30, 2014 at 12:14 PM
Let's say camera is your point A, and this is your point B.
Vector3 AB = B - A. Destination - Origin.
This is a direction and a distance. To have only the direction (and a distance of 1), you have to normalize it.
AB = AB.normalized OR AB.Normalize()
In your case :
Vector3 pos = camera.transform.position
Vector3 dir = (this.transform.position - camera.transform.position).normalized
Debug.DrawLine (pos, pos + dir * 10, Color.red, Mathf.Infinity);
Answer by HarshadK · Apr 30, 2014 at 12:11 PM
Check this out: Direction and Distance from One Object to Another
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Raycasting with Rotation of Camera 0 Answers
how to get a correct rotation 2 Answers
8 Directional sprite in 3d world, how to retrieve direction? 2 Answers