Get Camera Rotation Only On A Custom Axis

Hello,

I am interested in finding a way to rotate a part on the player’s camera’s axis. This is a similar idea to people wanting to only rotate a part based on soley the x, y, or z of some angle, however I am interested in finding a reliable way to do it based on a custom axis that has the ability to be angled (so not as simple as removing something like the y).
I’ve tried to find a way of doing this by means of subtracting the camera lookVector by the normal of my axis part (multiplied by the lookVector itself) which worked for any 90 angle but not for more complicated angles.

Any help is appreciated.
Thanks.


1 Like

Here’s this function I made. Let me know if it does/doesn’t work.
Z is forward for the yellow and purple parts, but for the red part I assumed that Y was the large flat side.

local function flattenToSurface(direction: CFrame, surface: CFrame): CFrame
	-- returns a positionless CFrame of the direction flattened to the surface.
	local transformed = surface:ToObjectSpace(direction)
	local x = Vector3.new(transformed.XVector.X, 0, transformed.XVector.Z).Unit
	local z = Vector3.new(transformed.ZVector.X, 0, transformed.ZVector.Z).Unit
	transformed = CFrame.fromMatrix(Vector3.zero, x, Vector3.yAxis, z)
	transformed = surface:ToWorldSpace(transformed)
	return transformed - transformed.Position
end

workspace.Part.CFrame = CFrame.new(workspace.Part.Position) * flattenToSurface(workspace.Target.CFrame, workspace.Surface.CFrame)
1 Like

Wow, that code actually works perfectly.
Thank you so much!

1 Like

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