How to make the camera sway like this?

hey all, im trying to recreate the camera sway that is found in a car racing game, which looks like this.
https://gyazo.com/83e131f52a374681f0104c9caa497717

what methods could I use to recreate it? im still trying to learn camera math and such and cant figure this out. I also wonder how to use the right stick on the gamepad to pan the camera around like this
https://gyazo.com/326ff28bb35ddb375ce482b089802acd

any help would be very much appreciated!

6 Likes

I’m with you, trying to learn camera manipulation. My best guess is a tween to the cars LookVector?

I might be wrong here, but to me this looks like the default camera behavior for the Follow CameraType mode.

You can use renderstepped and springs to kind of make the camera sway around.

Spring module: https://pastebin.com/WvPXPMbF

and then once you have the springs

-- local script obviously
local springs = require(path.to.module)
local sway = springs:create()

local function getSway(addition,speed,modifier)
    return math.sin(tick()*addition*speed)*modifier -- lol i got this from BlackShibe's fps tutorial
end

game:GetService("RunService").RenderStepped:Connect(function(dt)
    local swaySpeed,swayAmount = 0.5,0.1 -- the variables are what the names are

    local movementSway = Vector3.new(getBobbing(10,speed,modifier),getBobbing(5,speed,modifier),getBobbing(5,speed,modifier))

    sway:shove((movementSway / 50) * deltaTime * 10* velocity.Magnitude)

    local update = sway:update(dt)

    workspace.CurrentCamera.CFrame = workspace.CurrentCamera.CFrame * CFrame:Angles(0,update.Y,update.X)
end)

lots of this code(actually most of it) is from this tutorial: https://devforum.roblox.com/t/making-an-fps-framework-2020-edition/503318/1

and idk if it will work for your car but you can try

edit: like what @Rocky28447 said im pretty sure thats the follow cameratype mode

4 Likes

so things like “getBobbing” is a function inside the module? testing this out right now and integrating it with my car so i need to make sure this is correct

edit 1: also what does walkcycle mean in this context? my car doesnt use a walk cycle so i dont know what you mean by that. i also am confused with the getBobbing, do i have to make a funciton out of it? and the speed and mod, do i insert those values myself?

1 Like

whoops not walkcycle i forgot somethin

1 Like

ok so i tried your updated script and when sway.update() is called it errors at line 38 in the module because there is no number provided

whhhooooopppsssss put the dt into the update

god my brain needs halp

1 Like

it still is erroring.

image

also do i need to change the camera’s type to scriptable?

im not sure

you can try

can you paste ur code, all the stuff in the renderstep stuff

game:GetService("RunService").RenderStepped:Connect(function(deltaTime)
	local velocity = car.DriveSeat.Velocity
	    
	local swaySpeed, swayAmount = 0.5, 0.1 

    local movementSway = Vector3.new(getSway(10, 2, 1.5), getSway(5, 1, 1.1), getSway(5, 1, 1.1))

	sway:shove((movementSway / 50) * deltaTime * 10* velocity.Magnitude)
	    
	local update = sway.update(deltaTime)

    workspace.CurrentCamera.CFrame = workspace.CurrentCamera.CFrame * CFrame.Angles(0, update.Y, update.Y)
end)

it only errors when sway.update() is called

aaaa use
sway:update(deltaTime)

i had the same issue with fastcast once
replacing the . with a : should work

1 Like

change all the numbers in the code to suit ur needs

it works now, but it sways way too much, i want it only to sway when im turning the car with a or d, or using the left stick

is there a way i can use a camera part to integrate into a script?

ohhhh thats what you wanted ok

game:GetService("RunService").RenderStepped:Connect(function(deltaTime)
    local velocity = car.DriveSeat.RotVelocity.Y -- something so the script knows how much the car is turning
    
    local swayAmount = 0.1

    local movementSway = swayAmount * (velocity / 100) -- multipliying the amount to the velocity so more turn = more sway

    sway:shove(Vector3.new(movementSway,movementSway,0))
    
    local update = sway.update(deltaTime)

        workspace.CurrentCamera.CFrame = workspace.CurrentCamera.CFrame * CFrame.Angles(0, update.Y, update.Y)

end)

something like that

dinner xd

3 Likes

this is working the way i am integrating it into my car, thank you so much man, big help