Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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

2 comments

0

How do I get the orientation of this CFrame?

0

@nc2r, Updated post.

This discussion is preserved as it was. New answers, comments, and votes are closed.