Manipulate part's CFrame so that it moves along one axis while ignoring the rotation of another axis

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    I wish to change the position of the player’s camera by moving it in the direction of its local x-axis.

  2. What is the issue?
    When moving it in the direction of its local x-axis, it also applies the rotation the camera has on the z-axis, which I do not want. The camera should move independent of its pitch.

  3. What solutions have you tried so far?
    I tried experimenting with look vector and reading up a lot on CFrames, but I have no idea how to achieve this.

To better visualize my problem, assume this part is the camera:
image
This part has been rotated 45 degrees around the y-axis (green arrow) and 45 degrees around the z-axis (blue arrow)

If I were to move it along its x-axis, the red arrow, it would move downwards because I also rotated the part along the z-axis, the blue arrow.
What I want is to move it along the x-axis as if the z-axis was never transformed:
image
However, I do not want to actually change the z-axis rotation of the part, I just want to move it as if it was never changed.

This is what I have so far:

-- runs every render step, cam_pos is a global that describes position of camera,
-- x_angle describes the rotation of the x-axis, the pitch, which I want to ignore when mvoing forward
-- y_angle describes the rotation of the y-axis, the yaw, which determines the direction "forward" actually is
local function updateCamera(dt)
	if forward then
		-- this moves with the z-axis in mind
		cam_pos += camera.CFrame.LookVector
	end
	
	camera.CFrame = cam_pos * CFrame.fromOrientation(math.rad(x_angle), math.rad(y_angle), 0)
end

I would be happy to elaborate or provide more code samples if what I have now is too vague. Thanks for any help in advance.

2 Likes
--All this is equivalent to `return cf * CFrame.new(translation)`, just for illustration purposes.
function translateCFrameRelativeToItself(cf: CFrame, translation: Vector3): CFrame
    local relativeTranslation = cf:VectorToObjectSpace(translation)
    return cf + relativeTranslation 
end
1 Like