- Home /
how to find direction between two points
hey i need to know how to find the angle between two different points. For example if i had two different points pointA and pointB how would i find the global rotation required for pointA to face pointB. Thanks.
Answer by StephanK · Nov 02, 2010 at 09:31 AM
A point doesn't have an orientation, so a point can't "face" another point. If you want to make your GameObject face a certain point you can use transform.LookAt() to get the angle between two Vectors (not points) you can use Vector3.Angle()
EDIT: To get a ray from point A in the direction of point B you can use point A as origin and (pointB - pointA).normalized as the direction.
Thanks for the responses and sorry if i was vague with the information. $$anonymous$$ore specifically i wanted to create a ray that points from its origin to another object and i wanted to know how to go about getting the direction for the ray that is required when constructing the ray.
Thanks for the help everything works fine now, although i don't understand mathematically how normalizing the difference between to vectors gives the direction, but i guess that is extra information i don't need to know anyway :p. Thanks again for the help.
Well it maybe an extra information, but it's a useful one. Vectors have a direction and a length(magnitude). A direction is a so called unit vector, which has a length of 1. Normalizing a vector will give you its direction. $$anonymous$$athematically it divides the vector by its magnitude returnin a vector of length 1.
interesting, so if i have a vector of [1,1,1] its magnitude is is 1.7 and its normalized version is [1/1.7, 1-1.7, 1/1.7] or [.588,588,588]?. if i am correct so far, how would i go about converting this into a Euler or something more useful then just a vector?
Answer by Proclyon · Nov 02, 2010 at 09:46 AM
Speaking in general math/physics , without to much knowledge of how unity scripting supports the "getters" of this information I may be able to give you the information that you will need. How to get that information is tricky for me at the moment and I will have to direct you "HERE": http://unity3d.com/support/documentation/ScriptReference/index.html for that but I will try giving you some potential candidates to clear up search time, just excuse my lack of testing these idea's.
First of all there are 2 objects here.
Your object A and you Object B.
You want object A to look at Object B if I interpreted your question corrrectly , if not than I should have assumed you want to copy the rotation of object B onto Object A making them both face the same direction. To do that simply get transform.rotation information stored into a variable and create a new rotation quaternion for the A object with the following code sample.
Code for assumption 2 in which you want to copy the rotation of B to A:
Quaternion q = ObjectB.transform.rotation;
transform.rotation = q;
This may not work in C# though due to writing permissions on transforms requiring a new keyword in which you create a new (in this case) struct.
This quote from the Unity3D scripting references site explains why you don't want to go into details with Quaternions and simply want to copy paste:
Quaternion Struct
Quaternions are used to represent rotations.
They are compact, don't suffer from gimbal lock and can easily be interpolated. Unity internally uses Quaternions to represent all rotations. However, they are based on complex numbers and are not easy to understand intuitively. Thus you almost never access or modify individual Quaternion components (x,y,z,w); most often you would just take existing rotations (e.g. from the Transform) and use them to construct new rotations (e.g. to smoothly interpolate between two rotations). However, they are based on complex numbers and are not easy to understand intuitively. Thus you almost never access or modify individual Quaternion components (x,y,z,w); most often you would just take existing rotations (e.g. from the Transform) and use them to construct new rotations (e.g. to smoothly interpolate between two rotations).*
Excuse me if this sample will not work I haven't had the time to test it, but I hope it will be enough to get yon on your way!
If indeed you want to LOOK at the object instead of copy the rotation you would need to do this:
Assumption 1 sample:
Get the location of the target (transform of position B) Use the transform.LookAt member
function LookAt (target : Transform, worldUp : Vector3 = Vector3.up) : void //JS
transform.LookAt(target); //C#
Hope this is enough information for you to deal with the problem. It's not a great answer I know. But please help yourself to quick answers by adding information such as: Target platform, Compiler, code language used or any other information that can narrow down the search scope for the answer you need.