"Look behind you" feature for first-person camera

This line of thinking was what I needed. I already had a custom cam script that’s pretty similar to your example so this wasn’t very hard to solve

The current issues with this are that looking up or down while turning around pans like twice as fast and completely screws everything up, and that the camera controls are inverted while turned around.

First I made a camera rotation offset using this as the base https://devforum.roblox.com/t/how-would-i-offset-the-camera-with-rotation/1667248/22

I then did this to set the offset every frame

local oldOffset = CFrame.new()
local offset = CFrame.new()
local angleStorage = 0

RS.Stepped:Connect(function(time: number, dt: number)
	camera.CFrame *= oldOffset:Inverse()
	local offsetPanPerFrame = math.rad(360)*dt
	if UIS:IsKeyDown(Enum.KeyCode.Q) then
		angleStorage = math.min(angleStorage + offsetPanPerFrame, math.rad(180))
	else
		angleStorage = math.max(angleStorage - offsetPanPerFrame, 0)
	end
	offset = CFrame.fromEulerAnglesYXZ(0,angleStorage,0)
	camera.CFrame *= offset
	oldOffset = offset
end

hopefully i didn’t leave anything out of that. i tried to only keep the relevant bits of my camera script

edit:
If you want the player to still walk forward when looking behind, do this:

Comment out line 673 in BaseCamera:

--CameraUtils.setRotationTypeOverride(Enum.RotationType.CameraRelative)

You’re then going to have to edit your camera script so that the player’s rotation is manually set to the camera’s rotation (make sure to add this ABOVE any code relating to the camera rotation offset). This makes it so that looking behind you will still have the character facing forward.

Then, you’ll have to override the default movement code to use the HumanoidRootPart’s orientation instead of the camera’s:

--put this in StarterPlayerScripts
local playerScripts = script.Parent
local playerModule = require(playerScripts:WaitForChild("PlayerModule"))
local controls = playerModule:GetControls()

local character = game:GetService("Players").LocalPlayer.Character or game:GetService("Players").LocalPlayer.CharacterAdded:Wait()
local HRP = character:WaitForChild("HumanoidRootPart")

controls.moveFunction = function(player : Player, direction : Vector3, relative : boolean)
	local move = HRP.CFrame:VectorToObjectSpace(direction)
	player.Move(player, Vector3.new(move.X,move.Y,move.Z), true)
end
1 Like