Camera movement for camera shake

So i’m trying to make camera shake and i’m not sure how i should go about doing the actual movement. I tried tweening the camera but that interferes with player camera movement. Any suggestions on how i could do it without it interfering with player camera movement ?

2 Likes

You could use: Humanoid | Roblox Creator Documentation

And then repeatedly jitter it back and forth;

while wait() do
Humanoid.CameraOffset = Vector3.new(0,0,-1)
wait(0.1)
Humanoid.CameraOffset = Vector3.new(0,0,1)
wait(0.1)
end

Although i would reccomend this, some users have photosensitive disorders and you need to be respectfull of that.

1 Like

You could get the humanoid’s camera offset, and then use perlin noise to offset it. You could also use trig functions like sin, cos, tan, etc. with random numbers and lerping to create a fake smooth randomness. Sines, and cosines here are for dampening and allowing the new vector3 to be negative.

Here is a small example for how you would do it:

--// Services
local Players = game:GetService("Players")
local RunS = game:GetService("RunService")

--// Geting the humanoid
local plr = Players.LocalPlayer
local char = plr.Character

local hmoid = char:WaitForChild("Humanoid")

--// Trig functions
local sin, cos, tan = math.sin, math.cos, math.tan

--// Configs
local int = 0.25 --// Random number intensity
local lint = int --// Lerp intensity

local min = 0
local max = 5

--// The RenderStepped connection
local connection

--// Running the code every frame
connection = RunS.RenderStepped:Connect(function(dt)
	--// Random numbers
	local r1 = math.random(min, max) * int
	local r2 = math.random(min, max) * int
	local r3 = math.random(min, max) * int
	
	--// Making the camera smoothly go to the random vector3
	hmoid.CameraOffset = hmoid.CameraOffset:lerp(Vector3.new(sin(r1), cos(r2), sin(r3)), lint)
end)

--// You can stop the shaking by doing "connection:Disconnect()"
4 Likes

Try doing it with UI so It can’t affect the plr cam movement.

Let me specify my question, since no one understood it.

I want to know a method, which i could use to move the camera from one CFrame to another smoothly without it interfering with the player’s ability to rotate the camera with right click.

I tried using tweening but during a tween the player cannot rotate their camera, it only starts to affect after the tween has ended which essentially would create a big delay and i don’t want that.

I already know how camera shake works with the randomization and i can’t use Humanoid.CameraOffset as that doesn’t allow for rotation.

To do a Screen Shake with the camera you need to manipulate is the (x, y, z) with Humanoid.CameraOffset

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

I don’t think you can do that. Changing the camera’s CFrame will always overwrite the player’s camera movement, the closest you’ll get is to use Humanoid.CameraOffset, or by just locking the camera. You could add and subtract the camera’s CFrame, but it will not work very well. You also cannot rotate the camera forwards or backwards some x degrees without it rotating too much, you can only rotate it side to side. If you want to make it rotate from side to side, you could lerp the camera’s angle, and add normal camera shake using Humanoid.CameraOffset.

When you say “move the camera from one CFrame to another smoothly” It says like you want to make a cutscene, not camera shake.

You also never provided your code at all, which would make helping you do something which may or may not be impossible a lot easier.

I’ve modified my original script to slightly tilt the camera, it looks pretty good, but it’s not smooth, it can’t be smooth, since lerping the camera’s CFrame ruins everything, and locks the camera’s rotation as well.

--// Services
local Players = game:GetService("Players")
local RunS = game:GetService("RunService")

--// Geting the humanoid
local plr = Players.LocalPlayer
local char = plr.Character

local hmoid = char:WaitForChild("Humanoid")
local cam = workspace.CurrentCamera

--// Trig functions
local sin, cos, tan = math.sin, math.cos, math.tan
local rad = math.rad

--// Configs
local int = 0.25 --// Random number intensity
local lint = int --// Lerp intensity

local min = 0
local max = 5

local rotMax = 2 --// Max number for rotation

--// The RenderStepped connection
local connection

--// Running the code every frame
connection = RunS.RenderStepped:Connect(function(dt)
	--// Random numbers
	local r1 = math.random(min, max) * int
	local r2 = math.random(min, max) * int
	local r3 = math.random(min, max) * int
	
	--// Rotational random number
	local r4 = math.random(min, rotMax) * int
	
	--// Making the camera smoothly go to the random vector3
	hmoid.CameraOffset = hmoid.CameraOffset:lerp(Vector3.new(sin(r1), cos(r2), sin(r3)), lint)
	
	--// Camera rotation, not smooth, and most likely not what you're going for, but I have nothing else.
	cam.CFrame *= CFrame.Angles(0, 0, rad(tan(r4)))
end)

--// You can stop the shaking by doing "connection:Disconnect()"
1 Like

Easiest method is to use the EZ Camera Shake Module.

It’s easy to use and requires no maths at all. Plus there are multiple parameters that you can change on the shake.