How to make camera bobbling system on player movement
This works with first person camera enabled!
Firstly, I would like to thank @okeanskiy for displaying this tutorial on YouTube first before this article was made. I have just used his YouTube tutorial and posted it as a article on the Community Tutorials page
Hello everyone, I am going to show you how to make a camera bobbling system when player moves.
To begin with, add a LocalScript into StarterCharacterScripts and name it CameraBobble:
Setting Variables:
Write this code into the CameraBobble script:
local RunService = game:GetService("RunService")
This gets the service named RunService and stores it as a variable that we can use to trigger when the player moves. If you want to learn more about RunService click here: RunService.
We then want to store these two variable for setting the humanoid’s CameraOffset later on:
local Character = script.Parent
local Humanoid = Character.Humanoid
Making the camera bobbling when player moves:
When the player moves this event will fire so to do this we are going to write:
RunService.RenderStepped:Connect(function()
end)
The RenderStepped event fires every frame.
Next, we want to create more variables which are going to be used for the camera bobble!
We are going to set a variable for tick() and call it CurrentTime:
local CurrentTime = tick()
Nearly done!
After that we are going to get to the main part!
Drumroll please
Here it is. Paste this inside of the RenderStepped function:
if Humanoid.MoveDirection.Magnitude > 0 then
local BobbleX = math.cos(CurrentTime * 10) * 0.25
local BobbleY = math.abs(math.sin(CurrentTime * 10)) * 0.25
local Bobble = Vector3.new(BobbleX, BobbleY, 0)
Humanoid.CameraOffset = Humanoid.CameraOffset:Lerp(Bobble, 0.25)
else
Humanoid.CameraOffset = Humanoid.CameraOffset * 0.75
end
end)
Simple explanation what the script does:
Congratulations! You just made a camera bobble system!
The script you just wrote, or copy and pasted, now I’m just going to explain what the script does:
First we used RunService and RenderStepped to activate the camera bobbling. Then we inserted some variables for the camera bobbling. After setting the variables, we then set the humanoid’s CameraOffset to the camera bobbling variables to give it its effect.
And these are the results:
Normal player view:
First person view:
Hope this helped - @AustnBlox