How to rotate a part in the direction it's facing

How do I rotate a part depending on the direction it’s facing? If it’s facing different directions the axis to rotate it change, so how do I always rotate on the correct axis?

1 Like

part.CFrame *= CFrame.Angles(x,y,z)

this will rotate the part relative to the direction that it is facing, however you may need to change the axis/rotation

1 Like

I mean like you know how you use LookVector to a part forward, how would I do something similar for orientation?

1 Like

can you give an example of what you are trying to do?

1 Like

MB
image

As you can see in image 1 it’s the x-axis that’s making it rotate forwards, however in image 2 it’s the z-axis

1 Like

Use CFrame.fromAxisAngle. It takes a Vector3 and a number. The Vector3 is in local space (aka object space), so for example Vector3.new(1, 0, 0) will be 1 stud to the right of the part. Vector3.new(0, 0, 1) will be 1 stud in front of the part and so on.

If you want to rotate the character so it’s facing up/down, then you will want to rotate it around its RightVector. This is how you do that in code:

local axis = Vector3.new(1, 0, 0) -- the part will rotate around this vector in local space
local angle = 45 -- change this to how many degrees you want to rotate the part
part.CFrame = part.CFrame * CFrame.fromAxisAngle(axis, math.rad(angle))

After running this code on the HumanoidRootPart of the rig in the picture above, this is what it looks like:

The rig has been rotated 45 degrees upwards along its RightVector. It does not matter which way the rig is facing, using Vector3.new(1, 0, 0) will always make it rotate around its RightVector. I accidentally moved the camera in between screenshots, so the camera angle looks different.

More examples

Using <0, 1, 0> as your axis and -90 degrees as your angle, your character looks 90 degrees to the right:

Using <0, 0, 1> as your axis and 45 degrees as your angle, your character is tilted by 45 degrees:

1 Like