Camera shake for combat

im trying to make the camera shake to the left when i play the left punch animation do u have any suggestions that i could use to achieve that

you can use this code that is from another post

for i = 1, 20 do
    local x = math.random(-100,100)/100
    local y = math.random(-100,100)/100
    local z = math.random(-100,100)/100
    Humanoid.CameraOffset = Vector3.new(x,y,z)
    print(i)
    wait()
end

from here How can I make my screen shake? - Help and Feedback / Scripting Support - DevForum | Roblox

also remember not to post a subject that is already on the devforum and make sure to look for it first.

this is not the camera shake that i want ,that just creates like explosion shake

You can just adjust the values.

you can just change the math in it. If you want it more extreme you can remove the division and you can make the numbers in the math.random() much higher

example

for i = 1, 50 do
    local x = math.random(-9999,9999)
    local y = math.random(-9999,9999)
    local z = math.random(-9999,9999)
    Humanoid.CameraOffset = Vector3.new(x,y,z)
    print(i)
    wait()
end

no heres the thing it would just instead create a small explosion shake no matter what i just want to know how to could u make it lerp to left smoothly

like make the screen go to the left while shaking?

You are probably looking for this, EZ Camera Shake ported to Roblox
Read through it properly on how to understand it.

alright lemme take a look i think that should work

Something similar?

Its in StarterCharacterScripts

local humanoid = game:GetService("Players").LocalPlayer.Character.Humanoid
local camera = workspace.CurrentCamera

local shakeIntensity = 2.9 --intensity of the shake
local shakeSpeed = 0.1 --speed of the shake
local cameraShakeOffset = Vector3.new(-5, 0, 0) --amount of camera movement


function shakeCamera()
	local shakeOffset = Vector3.new(
		math.random(-shakeIntensity, shakeIntensity),
		math.random(-shakeIntensity, shakeIntensity),
		math.random(-shakeIntensity, shakeIntensity)
	)
	local originalOffset = humanoid.CameraOffset
	local currentTime = 0
	local duration = shakeSpeed
	while currentTime < duration do
		local delta = currentTime / duration
		humanoid.CameraOffset = originalOffset + shakeOffset * (1 - delta)
		camera.CFrame = camera.CFrame + cameraShakeOffset * (1 - delta) 
		currentTime = currentTime + wait()
	end
	humanoid.CameraOffset = originalOffset
	camera.CFrame = camera.CFrame - cameraShakeOffset -- Reset the camera position
end


shakeCamera()

I’d recommend using sin graphs for the best camera shake effect. I’m not too sure about the left and right camera shake but if you wanted a simple y-direction camera shake, here’s what you could do-

local camera = workspace.CurrentCamera
local player = game.Players.LocalPlayer
 -- Follow the comments for a smoother movement.

local a = .2 -- Decrease this value
local b = 2000 -- Increase this value by a lot

task.wait(5)

for i = 0, (2 * math.pi), .1 do -- Decrease to .01
	player.Character.Humanoid.CameraOffset += Vector3.new(0, (a * math.sin(b * i)), 0)
	task.wait(.1)
end

This is how a sin graph looks and what the code is doing is it takes all the values from 0 to 2pi and substitutes the i value into the function to create y-values that coordinate with the sin graph. So essentially, the camera’s y-position goes up and down as presented by the graph.

Of course this a really basic version which is not really smooth but you get the idea. Increase b to increase the number of times the camera goes up and down within the time period and increase a based on how high/low you want the camera to go.

5 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.