How do i fix this?

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

  1. What do you want to achieve? Keep it simple and clear!

A camera bobble that tilts smoothly.

  1. What is the issue? Include screenshots / videos if possible!

the script below that makes a camera bobbing effect have a problem when walking left or right without going forward it then instantly tilts the camera to the direction the player is going for some reason it doesnt do that while walking forward.

You need to check it out for yourself by pasting it in a local script inside of StarterCharacterScripts.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

There are no posts like this.

--CBob script with a tilt. org.by:IAmDevForumMember
task.wait(3) --StarterCharacterScripts.LocalScript
local Workspace = game:GetService("Workspace")
local RunService = game:GetService("RunService")
local LocalPlayer = game:GetService("Players").LocalPlayer
local UserInputService = game:GetService("UserInputService")
local Root = LocalPlayer.Character:WaitForChild("HumanoidRootPart")
local Mouse, Camera  = LocalPlayer:GetMouse(), Workspace.CurrentCamera
local angleX, x, y, tilt, vX, vY, sX, sY = 0, 0, 0, 0, 0, 0, 10, 10
local TouchEnabled = UserInputService.TouchEnabled
--* UserInputService.MouseIconEnabled = flase
local randomX, randomY = nil, nil
local function lerp(v1, v2, t)
	return v1 + (v2 - v1) * t
end --rewrite.by:2112Jay

RunService.RenderStepped:Connect(function(dt)
	local velocity = Root.Velocity.magnitude
	if velocity > 0.02 then dt *= 60 randomX = math.random(1, 2) randomY = math.random(1, 2) --x(10,15), y(5,10)
		vX = dt <= 2 and lerp(vX, math.cos(tick() * 0.5 * randomX) * (math.random(2, 5) / 200) * dt, 0.05 * dt) or 0 -- 5, 20
		vY = dt <= 2 and lerp(vY, math.cos(tick() * 0.5 * randomY) * (math.random(2, 5) / 200) * dt, 0.05 * dt) or 0 -- 2, 10
		
		Camera.CFrame *= CFrame.Angles(0, 0, math.rad(angleX))
			* CFrame.Angles(math.rad(math.clamp(x * dt, -0.15, 0.15)), math.rad(math.clamp(y * dt, -0.5, 0.5)), tilt)
			* CFrame.Angles(math.rad(vX), math.rad(vY), math.rad(vY * 10))
		
		tilt = math.clamp(lerp(tilt, -Camera.CFrame:VectorToObjectSpace((Root and Root.Velocity or 
			Vector3.new()) / math.max(LocalPlayer.Character.Humanoid.WalkSpeed, 0.01)).X * 0.05, 0.1 * dt), -0.05, 0.05)
		
		if not TouchEnabled and dt < 2 then
			angleX = lerp(angleX, math.clamp(UserInputService:GetMouseDelta().X / dt * 0.15, -2.5, 2.5), 0.25 * dt)
		end x = lerp(x, math.sin(tick() * sX) / 5 * math.min(1, sY / 10), 0.25 * dt)
		y = velocity > 1 and lerp(y, math.cos(tick() * 0.5 * math.floor(sX)) * (sX / 200), 0.25 * dt) or lerp(y, 0, 0.05 * dt)
		sX, sY = velocity > 12 and 20 or (velocity > 0.1 and 12 or 0), velocity > 0.1 and 18 or (velocity > 0.1 and 14 or 0)
	end
end)

This should work. Removed randomization of X & Y variables.

local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()
local Camera = game:GetService("Workspace").CurrentCamera
local UserInputService = game:GetService("UserInputService")
local Humanoid = Player.Character:WaitForChild("Humanoid")
local bobbing = nil
local func1 = 0
local func2 = 0
local func3 = 0
local func4 = 0
local val = 0
local val2 = 0
local int = 10
local int2 = 10
local vect3 = Vector3.new()

-- << Functions >> --

UserInputService.MouseIconEnabled = false

function lerp(a, b, c)
	return a + (b - a) * c
end

bobbing = game:GetService("RunService").RenderStepped:Connect(function(deltaTime)
	if script.Parent.Humanoid.MoveDirection.Magnitude > 0 then
		deltaTime = deltaTime * (script.Parent.Humanoid.WalkSpeed * 3.75)    
	else
		deltaTime = deltaTime * 60
	end

	if Humanoid.Health <= 0 then
		bobbing:Disconnect()
		return
	end

	local rootPart = Humanoid.RootPart
	local rootMagnitude = rootPart and rootPart.Velocity.Magnitude or 0
	local calcRootMagnitude = math.min(rootMagnitude, 50)

	if deltaTime > 3 then
		func1 = 0
		func2 = 0
	else
		func1 = lerp(func1, math.cos(tick() * 0.5 * math.random(10, 15)) * (math.random(5, 20) / 200) * deltaTime, 0.05 * deltaTime)
		func2 = lerp(func2, math.cos(tick() * 0.5 * math.random(5, 10)) * (math.random(2, 10) / 200) * deltaTime, 0.05 * deltaTime)
	end

	local bobbingRotation = CFrame.fromEulerAnglesXYZ(math.rad(func1), math.rad(func2), math.rad(func2 * 10))
	local rootRotation = CFrame.fromEulerAnglesXYZ(0, 0, math.rad(func3)) * bobbingRotation * CFrame.Angles(0, 0, math.rad(func4 * deltaTime * (calcRootMagnitude / 5)))

	Camera.CFrame = Camera.CFrame * rootRotation

	val2 = math.clamp(lerp(val2, -Camera.CFrame:VectorToObjectSpace((rootPart and rootPart.Velocity or Vector3.new()) / math.max(Humanoid.WalkSpeed, 0.01)).X * 0.08, 0.1 * deltaTime), -0.35, 0.2)
	func3 = lerp(func3, math.clamp(UserInputService:GetMouseDelta().X, -5, 5), 0.25 * deltaTime)
	func4 = lerp(func4, math.sin(tick() * int) / 5 * math.min(1, int2 / 10), 0.25 * deltaTime)

	if rootMagnitude > 1 then
		val = lerp(val, math.cos(tick() * 0.5 * math.floor(int)) * (int / 200), 0.25 * deltaTime)
	else
		val = lerp(val, 0, 0.05 * deltaTime)
	end

	if rootMagnitude > 12 then
		int = 20
		int2 = 18
	elseif rootMagnitude > 0.1 then
		int = 12
		int2 = 14
	else
		int2 = 0
	end

	vect3 = lerp(vect3, Camera.CFrame.LookVector, 0.125 * deltaTime)
end)
3 Likes