Setting Z rotational axis of CFrame doesnt work

Hello! I have had an issue with CFrames on the camera and I can’t seem to be able to solve it. The problem is that I try to set the CFrame Rotation Z to 0 but it doesn’t seem to be working

This is the script

rs.RenderStepped:Connect(function()
	local adderx = -((mouse.Y)-mouse.ViewSizeY/2)*sensitivity
	--print(-adderx)
	newx+=adderx
	local completex = math.rad(newx+att.WorldOrientation.X)
	
	local addery = -((mouse.X)-mouse.ViewSizeX/2)*sensitivity
	--print(-adderx)
	newy+=addery
	local completey = math.rad(newy)
	--print(xoffset)
	--cam.CFrame = CFrame.new(att.WorldPosition) * CFrame.Angles(att.WorldCFrame.Rotation.X, att.WorldCFrame.Rotation.Y, 0)
	cam.CoordinateFrame = CFrame.new(att.WorldPosition)
	--cam.CFrame = att.WorldCFrame
	--prevx += xoffset'
	--print(newx)
	if newx > nmaxangle then
		newx = nmaxangle
		print("Oops! too high")
	elseif newx < nminangle then
		newx = nminangle
		print("Oops! too low")
	end
	--newx=newx*math.abs(att.WorldOrientation.X)
	if not char.Humanoid.Sit and not char.HumanoidRootPart.Anchored then
		char.HumanoidRootPart.CFrame *= CFrame.Angles(0, cam.CFrame.Rotation.Y, 0)
	end
	--completex *= math.abs(cam.CFrame.XVector.Unit.Z-1)
	--print(cam.CFrame.LookVector.Y)


	cam.CFrame *= CFrame.Angles(completex, completey, math.rad(0)) -- OVER HERE


	print(completex, " we will catch you next time ")
	--print(completex)
	--* CFrame.Angles(xoffset, 0, 0)
	if adderx > maxcamspeed then
		adderx = maxcamspeed
	elseif adderx < mincamspeed then
		adderx = mincamspeed
	end
	wait(1/60)
end)

The script is a LOCALscript and it is located in StarterGui

There are values that were assigned outside of the script I’ve provided.

I have indicated where the issue lies. I hope someone can help me!

2 Likes

Try this line:

cam.CFrame = CFrame.Angles(completex, completey, math.rad(0)) + cam.CFrame.Position

Unfortunately that only made it worse and positioned my camera somewhere else.

Mind if I explain what the script is suppose to do? (i forgot to do that)

So this is a first person script that im trying to make. Im making a DOORS fangame. I have another script but it is starting to get old and I really need a new and improved script.

Yes, I’m struggling to understand.

I’ll try to record a video to show you

robloxapp-20241231-2349529.wmv (4.6 MB)
Here you go! (The problem is in the video too)

Sadly I don’t have OBS on this device so I had to record with roblox built in recorder

2 Likes

Happy newyear everybody! I hope everyone had a nice evening!

So what are you trying to do, in words? You want to reset the z rotation axis to 0?

You replaced this line:

cam.CFrame *= CFrame.Angles(completex, completey, math.rad(0)) -- OVER HERE

With this:

cam.CFrame = CFrame.Angles(completex, completey, math.rad(0)) + cam.CFrame.Position

Right?

Yes, and it made it even worse

When you set the CFrame, instead use :BindToRenderStep(), and set the priority to Enum.RenderPriority.Camera.Value - 1 .

I have only used that once before… could you explain how to use it?

rs:BindToRenderStep("action", Enum.RenderPriority.Camera - 1, function()
	local adderx = -((mouse.Y)-mouse.ViewSizeY/2)*sensitivity
	--print(-adderx)
	newx+=adderx
	local completex = math.rad(newx+att.WorldOrientation.X)

	local addery = -((mouse.X)-mouse.ViewSizeX/2)*sensitivity
	--print(-adderx)
	newy+=addery
	local completey = math.rad(newy)
	--print(xoffset)
	--cam.CFrame = CFrame.new(att.WorldPosition) * CFrame.Angles(att.WorldCFrame.Rotation.X, att.WorldCFrame.Rotation.Y, 0)
	cam.CoordinateFrame = CFrame.new(att.WorldPosition)
	--cam.CFrame = att.WorldCFrame
	--prevx += xoffset'
	--print(newx)
	if newx > nmaxangle then
		newx = nmaxangle
		print("Oops! too high")
	elseif newx < nminangle then
		newx = nminangle
		print("Oops! too low")
	end
	--newx=newx*math.abs(att.WorldOrientation.X)
	if not char.Humanoid.Sit and not char.HumanoidRootPart.Anchored then
		char.HumanoidRootPart.CFrame *= CFrame.Angles(0, cam.CFrame.Rotation.Y, 0)
	end
	--completex *= math.abs(cam.CFrame.XVector.Unit.Z-1)
	--print(cam.CFrame.LookVector.Y)


	cam.CFrame *= CFrame.Angles(completex, completey, math.rad(0)) -- OVER HERE


	print(completex, " we will catch you next time ")
	--print(completex)
	--* CFrame.Angles(xoffset, 0, 0)
	if adderx > maxcamspeed then
		adderx = maxcamspeed
	elseif adderx < mincamspeed then
		adderx = mincamspeed
	end
	wait(1/60)
end)

Like this.

Didn’t really fix the issue with Z not being set to 0 as supposed to

Darn I don’t get why its so difficult, it should be simple!

I notice here you have a wait(1/60), which is redundant, as it is at the end of the code block. You can remove it.

Also here you’re using CoordinateFrame, which is definitely deprecated, assuming cam is a Camera. Use CFrame. What is that line doing anyways?

I have done just that

Idk why i put it there

What is the line doing? You assign the CFrame twice without any waits, which is redundant.

What do you mean by that? I didn’t

You do these two lines after each other:

cam.CoordinateFrame = CFrame.new(att.WorldPosition)
cam.CFrame *= CFrame.Angles(completex, completey, math.rad(0)) 

You are assigning the same property

I think I’ll send the code I have been editing in the meantime…

rs:BindToRenderStep("CamWork", Enum.RenderPriority.Camera.Value - 1, function()
	--uis.MouseBehavior = Enum.MouseBehavior.Default
	--print(xoffset)
	--cam.CFrame = CFrame.new(att.WorldPosition) * CFrame.Angles(att.WorldCFrame.Rotation.X, att.WorldCFrame.Rotation.Y, 0)
	--cam.CFrame = att.WorldCFrame
	--prevx += xoffset'
	--print(newx)
	local targetcframe = cam.CFrame
	local xtarg,ytarg = targetcframe:ToEulerAnglesYXZ()
	if newx > nmaxangle then
		newx = nmaxangle
		print("Oops! too high")
	elseif newx < nminangle then
		newx = nminangle
		print("Oops! too low")
	end
	--newx=newx*math.abs(att.WorldOrientation.X)
	if not char.Humanoid.Sit and not char.HumanoidRootPart.Anchored and not char.Humanoid.PlatformStand then
		cameramodule.rotateroot(char, cam.CFrame.Rotation.Y)
		--char.Orientation = Vector3.new(0, cam.CFrame.Rotation.Y, 0)
		print("updatedChar")
		char.HumanoidRootPart.Transparency = 0
		char.HumanoidRootPart.LocalTransparencyModifier = 0
	end
	--completex *= math.abs(cam.CFrame.XVector.Unit.Z-1)
	--print(cam.CFrame.LookVector.Y)
	local adderx = -((mouse.Y)-mouse.ViewSizeY/2)*sensitivity
	--print(-adderx)
	newx+=adderx
	local completex = math.rad(newx+att.WorldOrientation.X)

	local addery = -((mouse.X)-mouse.ViewSizeX/2)*sensitivity
	--print(-adderx)
	newy+=addery
	local completey = math.rad(newy)
	--print(-adderx)
	--newz
	local completez = math.rad(newz+att.WorldOrientation.Z)
	cam.CFrame = CFrame.new(att.WorldPosition) * CFrame.Angles(completex, completey, math.rad(0)) -- Over here still
	print(completex, " we will catch you next time ")
	--print(completex)
	--* CFrame.Angles(xoffset, 0, 0)
	if adderx > maxcamspeed then
		adderx = maxcamspeed
	elseif adderx < mincamspeed then
		adderx = mincamspeed
	end
	--wait(1/60)
end)