Getting the camera's Y orientation CFrame

Hello everyone,
I have this code right here:

local directions = getDirectionsToProcess() 
local targetPosition = part.Position 

for _, direction in ipairs(directions) do
    local offset = (targetVelocity * step) * direction 
    targetPosition = targetPosition + offset 
end
part.Position = targetPosition

I’m trying to move a part using WASD keys, it works fine but I had one problem.
I want to rotate my part on the same direction as the player’s camera, exactly the orientation property. I tried to transform it to euleranglesXYZ by doing local x,y,z = CFrame.new():ToEulerAnglesXYZ() ; print(y) but it gave me another number.

So, how could I get that single property, thanks.
image

1 Like

Orientation is derived from the CFrame, it isn’t a real property itself, so changing it directly is unlikely to do what you want. If you want to rotate a part relative to the camera, you can use the camera axes to create a rotation CFrame and multiply the part’s CFrame by that.

local rollRotation = CFrame.fromAxisAngle(workspace.CurrentCamera.CFrame.RightVector, math.pi / 2)

part.CFrame = part.CFrame * rollRotation
2 Likes

Thanks, I will try that as soon as I get home! :smiley:

Okay so, unfortunately, your code doesn’t work.

The part just keeps rotating in different angles and it doesn’t move the way its orientation is supposed to make it go.

Any other solutions…?

Also, my plan is the change the part’s Y orientation to the camera’s Y angle ,so it can move in the same direction I move my camera to.

Eg:
part.Orientation = Vector3.new(0,camYorientation,0)

I’m not exactly sure what the code you provided does, but if you’re looking to get the camera’s Y orientation in world space you can use CFrame:ToEulerAnglesYXZ(). As the documentation says: “Returns approximate angles that could be used to generate the CFrame, if angles were applied in Z, X, Y order.” So, you can just get the Y angle and use that to rotate your part.

local Camera = workspace.CurrentCamera

local _, Y, _ = Camera.CFrame:ToEulerAnglesYXZ()
Part.Orientation = Vector3.new(0, math.deg(Y), 0) -- Angles are in radians and .Orientation uses degress so converting from radians to degrees
12 Likes

Works! Thank you so much!!

Finally its solved.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.