Hey, I have a script that bobs your camera as you move, and as expected, it quite literally has a seizure when you play with an FPS unlocker.
Is there any way I can modify my code to fix this issue?
Code for the bobbing script
while true do
wait();
if game.Players.LocalPlayer.Character then
break;
end;
end;
camera = game.Workspace.CurrentCamera;
character = game.Players.LocalPlayer.Character;
Z = 0;
damping = character.Humanoid.WalkSpeed / 2;
PI = 3.1415926;
tick = PI / 2;
running = false;
strafing = false;
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);
function mix(p3, p4, p5)
return p4 + (p3 - p4) * p5;
end;
while true do
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 then
tick = tick + character.Humanoid.WalkSpeed / 102;
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;
camera.CoordinateFrame = camera.CoordinateFrame * CFrame.new(math.cos(tick) / damping, math.sin(tick * 2) / (damping * 2), Z) * CFrame.Angles(0, 0, math.sin(tick - PI * 1.5) / (damping * 20));
end;
though, replacing this line with a wait() didn’t quite do anything, as a matter of fact, it broke the whole script and the camera won’t move at all now.
Out of curiosity, how does this code work? I see the new waited, frcheck, and d variables, but it looks like all of them don’t do anything besides update each other, so I don’t think they would have any effect on the code.
Bind it to render step and use delta time. Right now you are waiting for Renderstepped in a while loop, but you can use that to call a function instead of as just a wait and it passes in the amount of time since the last time it was called which you can multiply by values in your math to affect timing.
I posted this above, this is using delta time. You can also create a connection instead of using a loop.
(The step variable is the change in time, if you base your change in camera movement based on the change in time then the FPS doesn’t affect the rate of the camera manipulation)
Yeah, I forgot to embed the rest of the code in an if statement. I also changed ideal to 0.01 as 0.07 is too high. Basically it waits until the delta adds up to above the ideal time, then resets it.
while true do
game:GetService("RunService").Renderstepped:Wait()
end
You can do this instead
local function everyframe(deltaTime)
--Your code here. deltaTime is the time since it ran last.
end
game:GetService("RunService"):BindToRenderStep("everyframe", Enum.RenderPriority.Camera.Value + 1, everyframe) --Runs the function right after the camera updates on every frame