Camera bobble movement

Learn to make bobble camera so when you move, the camera shakes.

by Mark_GoodMan

Author Avatar

Hello, my name is Mark_GoodMan, a teenager programmers who makes tutorial for all of you, today i'll show you how to make Bobble camera. Like i said if you see grammar mistakes, i'll fix it soon.

Note: If you wanna copy the script without typing the whole thing again, just select the script and hit CTRL+C and you will copied the script you selected. Then just hit CTRL+V to paste the script.

DID YOU KNOW?

Most of RPG game, for example: Those who remain, when you move, your camera shakes from right to left, and when you stop moving, it goes back to normal, that is called Bobble, today i teach you how to make bobble camera for your game, a small feature could make a big impact to your game.

1. Adding script

Now the only simple thing you need to do is just go to StarterPlayer, go to StarterCharacterScripts and insert a LocalScript , then rename it to CameraBobble.

img|65x45

And that's basically all you need to add, no need to add anything else.

2. Scripting

So the next thing is scripting, now just enter the following script inside that LocalScript:

local runService = game:GetService("RunService")

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")

function updateBobbleEffect()
    local currentTime = tick()
    if humanoid.MoveDirection.Magnitude > 0 then -- if you start walking
        local bobbleX = math.cos(currentTime * 10) * .25
        local bobbleY = math.abs(math.sin(currentTime * 10)) * .25        

        local bobble = Vector3.new(bobbleX, bobbleY, 0)

        humanoid.CameraOffset = humanoid.CameraOffset:lerp(bobble, .25)
    else -- if you stop walking
        humanoid.CameraOffset = humanoid.CameraOffset * .75
    end
end

runService.RenderStepped:Connect(updateBobbleEffect)

And that's all the script you need to add, simple and easy.

EXPLAINING HOW THE SCRIPT WORKS

Let's learn how some of the scripts works:

if humanoid.MoveDirection.Magnitude > 0 then

This is how your camera will look how it normally looks like, when you join the game, it stays at that direction the camera is facing at you. If you remove this, the camera will continuously shaking non-stop, so i suggest keep it there and don't change the value. Also i tried to remove it, but it will give me an error anyways. But if you managed to not make an error, then the camera will just gonna shake non-stop.

 local bobbleX = math.cos(currentTime * 10) * .25
 local bobbleY = math.abs(math.sin(currentTime * 10)) * .25

This is how the camera will shakes at the perfect direction by using X and Y, also the math.sin(currentTime * 10) is a time it takes to make the camera goes from X to Y, and also goes from X or Y to the normal direction your camera facing at when your standing still. I recommend don't touch it and make it at 10 to make camera smoothly.

 humanoid.CameraOffset = humanoid.CameraOffset:lerp(bobble, .25)
    else -- if you stop walking
 humanoid.CameraOffset = humanoid.CameraOffset * .75

This is the speed of camera going back to the normal direction the camera faces, the lerp(bobble, .25) is how the camera goes from X to Y like the math.cos from bobbleX and bobbleY, you can change the CameraOffSet (not the CameraOffset:lerp) from 75 to 50 or 100, but it may be too quick or too slow to move camera direction to the normal direction.

That's all you need to learn how the script works.

3. Editing script

So to not cause any error to the script, you can edit how your camera shakes, by changing the bobbleX and bobbleY, if the value is higher, then your camera will shakes even farther, setting lower will make it shakes shorter. The recommended value is 25 for both X and Y

local bobbleX = math.cos(currentTime * 10) * .75 -- Or 10
local bobbleY = math.abs(math.sin(currentTime * 10)) * .75 -- Or 10

Setting to 75 makes it shakes farther, setting to 10 makes it shakes shorter. Always make bobbleY the same value as bobbleX, if the value is different to each other, it will cause errors, also, setting value over 75 won't make it shakes even farther now, 75 is the maximum value that it can shakes.

And that's basically all you need to, the easiest and the simple method to impact your game, short and easy to understand, now you can run the game and enjoy camera shaking from left to right depends on the value of bobbleX and bobbleY you set at.

That's all you need to learn how the script works.

Thank you Lua Learning, the best place to learn Lua to make success for your game, i will check on this more often to see my reputation and reviews, last time viewed was 1/14/2020.

View in-game to comment, award, and more!