x


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.

more ▼

asked Nov 02, 2010 at 08:20 AM

Gian Hancock gravatar image

Gian Hancock
48 1 2 7

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Nov 02, 2010 at 09:31 AM

StephanK gravatar image

StephanK
6.5k 116 103 148

Thanks for the responses and sorry if i was vague with the information. More 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.

Nov 03, 2010 at 06:18 AM Gian Hancock

I edited my original answer.

Nov 03, 2010 at 08:21 AM StephanK

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.

Nov 03, 2010 at 10:23 AM Gian Hancock

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. Mathematically it divides the vector by its magnitude returnin a vector of length 1.

Nov 03, 2010 at 03:49 PM StephanK

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?

Nov 04, 2010 at 06:31 AM Gian Hancock
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Nov 02, 2010 at 09:46 AM

Proclyon gravatar image

Proclyon
1.5k 30 23 46

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1459
x1022
x486
x446
x199

asked: Nov 02, 2010 at 08:20 AM

Seen: 44623 times

Last Updated: Aug 02, 2013 at 09:54 PM