How to change a single axis of a CFrame?

Hello, I’m trying to change the X value of a CFrame’s rotation to 0. I successfully have done this multiplying a CFrame.New() to CFrame.Angles(). The only issue is that for some reason the Z value also gets set to 0

I ONLY WANT THE X VALUE SET TO 0.
here is the code:

local CFra = CFrame.new(camera.CFrame.Position) * CFrame.Angles(0, camera.CFrame.Rotation.Y, camera.CFrame.Rotation.Z)
local CFRATwee = TS:Create(camera, TweenInfo.new(0.1), {CFrame = CFra})
CFRATwee:Play()
1 Like

You can shift a part 3 studs to the right from its front-facing orientation:

workspace.Part.CFrame = workspace.Part.CFrame + workspace.Part.CFrame.RightVector*3

There’s also LookVector and UpVector. You can also multiply by a negative number to move it the other way.

I am trying to set a single value of a CFrame’s rotation to 0 but not change any other axis at all. I believe you only read the title and not the actual issue. How do I set the X rotation value of a CFrame to 0 but keep all the other values. The method I showed in the post works but for some reason also sets the Z value to 0

I understand now. And I’ve never done this before so I’ll take a look.

CFrame.fromEulerAnglesXYZ(0, 0, 0)

I just told you I wanted to change only the X rotation value. You set all 3 axises to 0. I want to only change 1 of them.

local X,Y,Z = workspace.EYE.CFrame:ToEulerAngles(); workspace.EYE.CFrame = workspace.EYE.CFrame*CFrame.Angles(-Y,0,0); print(workspace.EYE.CFrame:ToEulerAngles())

This code seemed to give some consistent results. No idea why.

as I can see from the code, you are changing 2 values to 0. For my specific issue, I’m trying to change 1 value.

I would like the ask, I noticed that you put the Y cordinate as the X cordinate in CFrame.Angles(). Is there a specific way to write CFrame.Angles()? I thought it was just XYZ like vector3. In your example you did YXZ or something

So by multiplying CFrame.Angles(0.1, 0, 0), it will rotate along the X axis. However I don’t know how ToEulerAngles functions work. So I just did some guessing. I don’t think it works tho.

you are confusing Setting the rotation with multiplying the rotation. when you do:

CFrame.new(positionn) * CFrame.Angles()

it sets the angles of a CFrame.

when you do

CFrame * CFrame.Angles()

it adds to the angles by whatever angles you put.

In my case I am setting the angles so I would do

CFrame.new(position) * CFrame.Angles()

Remember my goal is to set the X value of CFrame.Angles() to 0

in order to do that we would have to put the values it was before. so I would do

local X,Y,Z = CFrame:ToEulerAngles()
CFrame.new(position) * CFrame.Angles(0, Y, Z)

Now i personally think I have it figured out. The only issue is that this code (which should work) doesnt work. As you can see I’m only changing the X value to 0. Even with that, It changes both X and Z

1 Like

It sounds like you are getting into gimbal lock territory when rotating only one of the axes by exactly 90 degrees. Here’s a link that explains what causes the issue: Gimbal Lock Explanation And How To Work With It | by I-Ming HSU | Dev Genius

1 Like

I read the article but I still need to be able to do what I’m doing. I cant just not do it. What is the solution to this roblox bug that you have told me? How do I bypass roblox not being smart enough to not get into a “gimbal lock” this shows that even roblox makes ERRORS. I believe things such as the “GIBOL LOCK” which PREVENTS ITS DEVS from making BASIC ROTATIONS is the reason there is no good games on roblox. Roblox does not let their developers make games because they think its funny. its not funny roblox. how do I fix this thing roblox made to stop good games being on its platform?

Sorry, I forgot to mention that Gimbal Lock has been covered and solved in other posts on the forums.
Just type in ‘gimbal lock’ in the Search tool up top.

local X,Y,Z = workspace.EYE.CFrame:ToOrientation(); workspace.EYE.CFrame = CFrame.new(workspace.EYE.Position)*CFrame.fromEulerAngles(0, Y, Z, Enum.RotationOrder.YZX)

Best thing I could figure out to do what you want.

I watched some youtube videos on the gimbal lock and I noticed that if I removed a axis then the gimbol lock wouldnt be possible. bassically what I was doing was that I was making a plugin that allowed me to rotate my studio camera with the arrow keys. I did this since on laptop, its very hard to rotate the camera with a trackpad. Anyways as I said before, I just removed the Y axis. Here is the final plugin code:

local UIS = game:GetService('UserInputService')
local camera = workspace.Camera
camera.CameraType = "Scriptable"
local RotatingUp = false
local RotatingDown = false
local RotatingLeft = false
local RotatingRight = false
local speed = 0.001
local rate = 0.08
local ts = game:GetService("TweenService")

local Z = 0
local Y = 0

local function GoUp()
	while RotatingUp do
		Y = Y + rate
		local tween = ts:Create(camera, TweenInfo.new(speed), {CFrame = CFrame.new(camera.CFrame.Position) * CFrame.fromEulerAnglesYXZ(Y,Z,0) }) 
		tween:Play()
		tween.Completed:Wait()
	end
end

local function GoDown()
	while RotatingDown do
		Y = Y - rate
		local tween = ts:Create(camera, TweenInfo.new(speed), {CFrame = CFrame.new(camera.CFrame.Position) * CFrame.fromEulerAnglesYXZ(Y,Z,0) }) 
		tween:Play()
		tween.Completed:Wait()
	end
end

local function GoLeft()
	while RotatingLeft do
		Z = Z + rate
		local tween = ts:Create(camera, TweenInfo.new(speed), {CFrame = CFrame.new(camera.CFrame.Position) * CFrame.fromEulerAnglesYXZ(Y,Z,0) }) 
		tween:Play()		
		tween.Completed:Wait()
	end
end

local function GoRight()
	while RotatingRight do
		Z = Z - rate
		local tween = ts:Create(camera, TweenInfo.new(speed), {CFrame = CFrame.new(camera.CFrame.Position) * CFrame.fromEulerAnglesYXZ(Y,Z,0) }) 
		tween:Play()		

		tween.Completed:Wait()
	end
end

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Up then
		RotatingUp = true
		task.spawn(GoUp)
	elseif input.KeyCode == Enum.KeyCode.Down then
		RotatingDown = true
		task.spawn(GoDown)
	elseif input.KeyCode == Enum.KeyCode.Left then
		RotatingLeft = true
		task.spawn(GoLeft)
	elseif input.KeyCode == Enum.KeyCode.Right then
		RotatingRight = true
		task.spawn(GoRight)
	end
end)

UIS.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Up then
		RotatingUp = false
	elseif input.KeyCode == Enum.KeyCode.Down then
		RotatingDown = false
	elseif input.KeyCode == Enum.KeyCode.Left then
		RotatingLeft = false
	elseif input.KeyCode == Enum.KeyCode.Right then
		RotatingRight = false
	end
end)

1 Like

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