Player moves wrong because of camera bobbing

I got a camera bobbing script and another one that shows the Player’s body. Problem is, the player doesn’t move straight and sways… A little too much

LocalScript 1: (body)

local UIS = game:GetService("UserInputService")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

local debounce = false

for _, v in ipairs(Character:GetChildren()) do
	if not v:IsA("BasePart") or v.Name == "Head" then continue end
	v.LocalTransparencyModifier = v.Transparency
	v:GetPropertyChangedSignal("LocalTransparencyModifier"):Connect(function()
		v.LocalTransparencyModifier = v.Transparency
	end)
end

UIS.InputBegan:Connect(function(input, GPE)
	if GPE then return end
	
	if input.KeyCode == Enum.KeyCode.P then
		if debounce then return end
		if Player.CameraMode == Enum.CameraMode.LockFirstPerson then
			debounce = true
			Player.CameraMode = Enum.CameraMode.Classic
			Player.CameraMaxZoomDistance = 15
			Player.CameraMinZoomDistance = 10
			Humanoid.CameraOffset = Vector3.new(0, 0, 0)
			wait(1)
			debounce = false
		elseif Player.CameraMode == Enum.CameraMode.Classic then
			debounce = true
			Player.CameraMode = Enum.CameraMode.LockFirstPerson
			Player.CameraMaxZoomDistance = 0.5
			Player.CameraMinZoomDistance = 0.5
			Humanoid.CameraOffset = Vector3.new(0, 0.5, 0)
			wait(1)
			debounce = false
		end
	end	
end)

LocalScript 2: (Bobbing)

local tick = tick

camera = game.Workspace.CurrentCamera;
plr = game.Players.LocalPlayer
character = plr.Character or plr.CharacterAdded:Wait();
hum = character:WaitForChild("Humanoid");
Z = 0;
damping = character.Humanoid.WalkSpeed / 3; --[[This is pretty much the strength of the camera bobbing 
you are free to change the character.Humanoid.WalkSpeed / 2 to either a whole number or change the division]]--
PI = 3.1415926;
tick = PI / 2;
running = false;
strafing = false;
IsMoving = false;

repeat wait() until game:IsLoaded()

character.Humanoid.Strafing:connect(function(p1)
	strafing = p1;
end);
character.Humanoid.Jumping:connect(function()
	running = false;
end);
character.Humanoid.Swimming:connect(function()
	running = false;
end);
character.Humanoid.Running:connect(function(p2)
	if p2 > 0.1 then
		running = true;
		return;
	end;
	running = false;
end);
character.Humanoid.Died:connect(function()
	running = false;
end);

character.Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
	if character.Humanoid.MoveDirection.Magnitude == 0 then
		IsMoving = false
	else
		IsMoving = true
	end
end)

function mix(p3, p4, p5)
	return p4 + (p3 - p4) * p5;
end;
while true do
	local step = game:GetService("RunService").RenderStepped:wait();
	fps = (camera.CoordinateFrame.p - character.Head.Position).Magnitude;
	if fps < 0.52 then
		Z = 0;
	else
		Z = 0;
	end;
	if running == true and strafing == false and IsMoving == true then
		tick = tick + character.Humanoid.WalkSpeed / 102 * (30*step);
	else
		if tick > 0 and tick < PI / 2 then
			tick = mix(tick, PI / 2, 0.9);
		end;
		if PI / 2 < tick and tick < PI then
			tick = mix(tick, PI / 2, 0.9);
		end;
		if PI < tick and tick < PI * 1.5 then
			tick = mix(tick, PI * 1.5, 0.9);
		end;
		if PI * 1.5 < tick and tick < PI * 2 then
			tick = mix(tick, PI * 1.5, 0.9);
		end;
	end;
	if PI * 2 <= tick then
		tick = 0;
	end;
	
	if hum.WalkSpeed <= 10 then
		camera.CoordinateFrame = camera.CoordinateFrame * CFrame.new(math.cos(tick) / damping, math.sin(tick * 2) / (damping * 1.5), Z) * CFrame.Angles(0, 0, math.sin(tick - PI * 1.5) / (damping * 100));
	elseif hum.WalkSpeed >= 20 then
		camera.CoordinateFrame = camera.CoordinateFrame * CFrame.new(math.cos(tick) / damping, math.sin(tick * 2) / (damping * 0.25), Z) * CFrame.Angles(0, 0, math.sin(tick - PI * 1.5) / (damping * 25));
	end
end;

This line right here, the problem is your walkspeed is slow, making the damping less, less damping more bobbing. You can set it to times 2 instead of dividing it by 3.

Thanks! This worked!
The “Character Swaying” is actually accidental, but it seems to look good!

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