Setting CFrame orientation not working

I want to figure out why the Z value in the rotation changes even though I set it to 0

image

I have looked at many CFrame explanations but I don’t see how they can help my issue.
Ive tried setting it to 0 afterwards but that breaks things more.

Im looking for a way to make just the Z rotation value 0.

my current code is this:

ContextActionService:BindAction("MouseMovement",function(name,state,input:InputObject)
	local delta = input.Delta
	if delta == blank then return end
	print(delta)
	Camera.CFrame += delta
	script.Parent.Base.CameraAttachment.WorldCFrame = script.Parent.Base.CameraAttachment.WorldCFrame * CFrame.fromEulerAngles(math.rad(delta.Y * -1),math.rad(delta.X * -1),0) -- This is the issue
	print(script.Parent.Base.CameraAttachment.WorldCFrame.Rotation)
end,false,Enum.UserInputType.MouseMovement)

Keep in mind this is for a drone camera rotation script in my game.
(and yes my code is a little messy)

The fourth argument for CFrame.fromEulerAngles defaults to being applied in XYZ order whereas Roblox orientation is applied in YXZ order. You would have to set the 4th arg to Enum.RotationOrder.YXZ, or just use fromEulerAnglesYXZ.

Im pretty sure that the 0 is still in the right place

its in the Z area I believe?

It would still to change the value of the rotation on the Z axis

Here’s an example of a part randomly oriented, top is in XYZ order, bottom is in YXZ order
image

Here’s an article on that:

Im asking if there is a way to only change the Z rotation.

Yes, you would need to use the right rotation order to get the result you want though.

CFrame.fromEulerAngles(math.rad(delta.Y * -1),math.rad(delta.X * -1),0, Enum.RotationOrder.YXZ)

or

CFrame.fromEulerAnglesYXZ(math.rad(delta.Y * -1),math.rad(delta.X * -1),0)
1 Like

would it then be this?

script.Parent.Base.CameraAttachment.WorldCFrame = script.Parent.Base.CameraAttachment.WorldCFrame * CFrame.fromEulerAnglesYXZ(math.rad(delta.Y * -1),math.rad(delta.X * -1),0)

because for some reason this also doesnt work.

Demonstration video:

Oh you know what, I completely overlooked the fact you’re multiplying it by an attachment’s WorldCFrame

local targetCFrame = script.Parent.Base.CameraAttachment.WorldCFrame

-- first we get rid of the Z axis 

local x, y = targetCFrame:ToEulerAnglesYXZ()

script.Parent.Base.CameraAttachment.WorldCFrame = CFrame.new(script.Parent.Base.CameraAttachment.WorldPosition) * 
    CFrame.fromEulerAnglesYXZ(x, y, 0) * 
    CFrame.fromEulerAnglesYXZ(math.rad(-delta.Y), math.rad(-delta.X), 0)

If this doesn’t work, you might have to reconstruct the CFrame entirely using the attachment’s parent’s CFrame

1 Like

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