How would I add camera bobble to a first person script?

Hello, I have been wondering of how to achieve camera bobble inside a first person script.
I have searched for tutorials but none worked since the first person script has some functions that block the camera offset from changing.

Here’s the first person script:

local player = game.Players.LocalPlayer
player.CameraMode = Enum.CameraMode.LockFirstPerson
local char = player.Character
local RunService = game:GetService("RunService")
local Mouse = player:GetMouse()

Mouse.Icon = "http://www.roblox.com/asset/?id=569021388"

char.Humanoid.CameraOffset = Vector3.new(0, 0, -1)

for i, v in pairs(char:GetChildren()) do
	if v:IsA("BasePart") and v.Name ~= "Head" then
		v.LocalTransparencyModifier = v.Transparency
		v:GetPropertyChangedSignal("LocalTransparencyModifier"):Connect(function()
			v.LocalTransparencyModifier = v.Transparency
		end)
	end
end

local function CameraEffect()	
	local ray = Ray.new(char.Head.Position, ((char.Head.CFrame + char.Head.CFrame.LookVector * 2) - char.Head.Position).Position.Unit)

	local IgnoreList = char:GetChildren()

	local hit, pos = game.Workspace:FindPartOnRayWithIgnoreList(ray, IgnoreList)

	if hit then
		char.Humanoid.CameraOffset = Vector3.new(0, 0, -(char.Head.Position-pos).magnitude)
	else
		char.Humanoid.CameraOffset = Vector3.new(0, 0, -1)
	end
end
RunService:BindToRenderStep("CameraRayCast", Enum.RenderPriority.Camera.Value, CameraEffect)
2 Likes

From what I can think of, I would recommend to have a part which randomly moves inside the players head and instead have the camera be viewed from the part instead. The only issue with my method is it would likely mean any accessories on the head are visible.

1 Like

You can achieve the bobbing effect by using math.noise or sinusoidal wave manipulation & character velocity. You would want the first person script to modify the camera CFrame after it has performed it’s other instructions in the CameraEffect() call. Example:

local cam = workspace.CurrentCamera; local camCF = cam.CFrame
cam.CFrame = (camCF * modificationCFrame)

Because the camera CFrame is reset by the core camera code, modifying it after the fact per frame allows you to control the camera. There are lots of tutorials for bobbing on YouTube, just apply the logic and you should be set

1 Like

The issue is, the CFrame is already set by the RayCast or so. I have tried some scripts but none worked.
But thanks for suggestions!

From the code you provided, unless there’s more I can’t see, it’s definitely not being set. It’s just the Humanoid’s CameraOffset property, which is a factor in camera manipulation, but it’s not conflicting with my suggestion. Here’s some code I have on hand that should accomplish what I’m describing

MIN_SHAKE_VELOCITY = 5

PI = 3.1415926;
TAU = 0;

wave_multiplier = 1.8;
shake_scale = 190

function mix(p3, p4, p5)
	return p4 + (p3 - p4) * p5
end

local function bob_step()
	local realCamCF = workspace.CurrentCamera.CFrame
	local velocity = char.Head.Velocity.Magnitude
	
	if velocity > MIN_SHAKE_VELOCITY then
		TAU = TAU + velocity / shake_scale;
	else
		if TAU > 0 and TAU < PI / 2 then
			TAU = mix(TAU, PI / 2, 0.9);
		end;

		if PI / 2 < TAU and TAU < PI then
			TAU = mix(TAU, PI / 2, 0.9);
		end;

		if PI < TAU and TAU < PI * 1.5 then
			TAU = mix(TAU, PI * 1.5, 0.9);
		end;

		if PI * 1.5 < TAU and TAU < PI * 2 then
			TAU = mix(TAU, PI * 1.5, 0.9);
		end;
	end;

	if PI * 2 <= TAU then
		TAU = 0;
	end;
	
	realCamCF = realCamCF
		* CFrame.new(math.cos(TAU) / damping * wave_multiplier, math.sin(TAU * 2) / (damping * 2) * wave_multiplier, 0)
		* CFrame.Angles(0, 0, math.sin(TAU - PI * 1.5) / (damping * 20) * wave_multiplier);

	workspace.CurrentCamera.CFrame = realCamCF
end
4 Likes