Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

LookVector between two positions?

Asked by 8 years ago

I'm making a compass, and I need to find the lookvector of the position of my character and the position of a part, so basically the lookvector as if one Vector3 was looking at the end Vector3. Once I have this I can just use math.atan2(Pos.x, Pos.z) to get my degree and put it onto the compass gui. Any help?

1 answer

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

First, you can do exactly that by getting the lookVector of a CFrame that points the way you want. Luckily, there's a convenient constructor for that:

local cf = CFrame.new( from, to )
-- a CFrame located AT from, pointing TOWARDS to

local dir = cf.lookVector

Of course, this indirection isn't necessary.


To get from vector A to vector B, you have to travel a certain amount. That "travel" is also a vector, called a displacement. You compute displacement by subtracting: d = B - A. (This makes since A + d = A + B - A = B).

A displacement has a length -- the distance between A and B. For many things, you want to ignore the distance (for example, in the compass) -- you just want the direction.

Strictly speaking, that's not possible with a vector. But you can get a unit vector in the same direction -- a vector with the same direction, but unit (1) length.

local dir = (to - from).unit
Ad

Answer this question