Camera Movement when Moving

So I found this free model of an AK and I saw something very interesting. When you walk crouched with the Gun, the camera make a movement, from up to down and from down to up.

Here’s the video of what I’m saying: (Sorry for bad quality)

I tried to find the Code for that camera movement, but I didn’t find anything. Anyone knows how to do it?

1 Like

This type of camera movement would be view bobbing or camera bobbing. There are multiple scripts on the Toolbox which have something similar to this.

Most of the time people use Sine wave movement to do camera bobs like this, if you have taken Calculus you should be familiar with how a Sine wave works, here is a visual example of what is going on in the math behind the code.

With this knowledge we can create a camera bobbing script, changing only the amplitude and the wavelength in order to get the amount of bobbing that we want, and the speed of the bobbing that we are desiring.

-- Created by TDK on 10/27/2019
RunService = game:GetService("RunService")
Camera = workspace.CurrentCamera
Distance = 10
Speed = 10

game.Players.LocalPlayer.CharacterAdded:Connect(function()
	RunService.RenderStepped:Connect(function()
		Camera.CoordinateFrame = Camera.CoordinateFrame*CFrame.Angles(0,0,math.rad(Distance*math.sin(tick()*Speed)))
	end)	
end)

This code creates a result like this:

https://gyazo.com/28b4e451654d3ca72ea2a8b6b7bff0f8

Of course, if you would like it to only be while you are crouching you will have to add some code to have the script only run whenever some certain parameters are met, if you have any trouble please let me know!

17 Likes

I want to know how if can run the script just when i walk, not when i get idle.

Then you will have to add a function to detect when the player is running.

This can be done using the Running event:

game.Players.LocalPlayer.Character.Humanoid.Running:Connect(function(Speed)
if Speed > 0 then
--Player is walking, move camera
else
--Player is standing still, stop moving camera
end
end)
1 Like

It won’t be easier & better to use Humanoid.MoveDirection.magnitude?

I mean you could use magnitude, but I just prefer using the event.

Is there a way to smoothly move the camera back once walking stops? Right now I have a system where when you walk it starts moving the camera, but when i stop walking it abruptly moves the camera back to its position.

(also yes i know this topic is over a year old)

never mind, i found out out how to do it already. just make a new cframe value, lerp it using this

		Camera.CoordinateFrame = Camera.CoordinateFrame*CFrame.Angles(0,0,math.rad(Distance*math.sin(tick()*Speed)))

obv you’d have to adjust it for lerping

and then multiply the cam’s cframe by the cam’s cframe and the new cframe value

1 Like