So let’s say I have a part, and I want it to rotate in a given direction. How would I turn this unit vector into a rotation?
If you simply want to derive a cframe rotation matrix from a unit vector do something like:
CFrame.lookAt(Vector3.new(0, 0, 0), directionVector)
You could also take a trigonometric approach and define 2 axes of rotation which could create a cframe as 2 of the 3 axes in a euler angle.
The “Orientation” property of “Part” instances is calculated using a Vector3 value.
What do you mean, exactly? Do you want to cause it to slowly rotate over time such that its angular velocity matches “this unit vector”? Or do you want the unit vector to define the axis of rotation?
To be more specific, my game has a rolling mechanic. The rolling mechanic works by taking the humanoid’s MoveDirection and moving the character in that direction. I also want to make the camera roll in the same direction, which is where I don’t know what to do.
Is it a first person game? So e.g. if rolling forwards, it would look like pitching down 360 degrees over a short time span?
If that’s the case, you’ll want to rotate the camera around the vector that is perpendicular to both the movement direction and the up direction (0, 1, 0). You can compute that vector as Vector3.new(0, 1, 0):Cross(moveDirection)
. You can turn that into a CFrame that you can transform the camera CFrame by using CFrame.FromAxisAngle, e.g. something like
local angleAxis = Vector3.new(0, 1, 0):Cross(moveDirection)
local numSteps = 100
local stepSize = math.rad(360) / numSteps
for i = 0, numSteps - 1 do
camera.CFrame *= CFrame.FromAxisAgnle(angleAxis, stepSize)
wait()
end