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

How do I find the rotation implied by a unit vector?

Asked by
nc2r 117
3 years ago

A unit vector is literally a direction. They are LookVectors, and are found by RayCasts, or by using (Position1-Position2).Unit to obtain the direction between one point and another. A unit vector is a direction. However, how can I obtain a rotation from a unit vector? I need to set an orientation of a part so the part will face the direction of the unit vector? How can I do that?

1 answer

Log in to vote
2
Answered by 3 years ago
Edited 3 years ago

The easiest solution to this would be to point the part using CFrames. The second argument of a CFrame may be a position that the CFrame's direction values will be 'pointed' at, which is perfect in your case.

Here's an example:

part.CFrame = CFrame.new(part.Position, part.Position + unit)

However, keep in mind that this wont output the rotation directly, it'll only rotate the CFrame.

How to get rotation from a CFrame

By simply subtracting the Position component of a CFrame from itself, we can get the rotational values.

local ang = Vector3.new(part.CFrame - part.CFrame.Position) -- Gets rotation
ang = Vector3.new(math.deg(ang.X), math.deg(ang.Y), math.deg(ang.Z)) -- Converts rotation into degrees
0
How do I get the orientation of this CFrame? nc2r 117 — 3y
0
@nc2r, Updated post. Vinceberget 1420 — 3y
Ad

Answer this question