Screen Shake to Audio

I’m basically trying to make the screen shake in sync with a sound playbackloudness, I can use this function but is there anyway to convert it to use x,y,z axis for camera cframes?

function soundPlayback()
	local Camera = workspace.CurrentCamera -- reference this somewhere at the top to cache
	local Tween = game:GetService("TweenService") -- same with this
	local Info = TweenInfo.new(0.10, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut) -- also this.

	local Properties

	if (sound.PlaybackLoudness/1000) >= 0.30 and Camera.FieldOfView < 74 then
		Properties = {FieldOfView = 68 + (sound.PlaybackLoudness/100)}
	else
		Properties = {FieldOfView = 68 - (sound.PlaybackLoudness/1000)}
	end

	local T = Tween:Create(Camera, Info, Properties)
	T:Play()
end
6 Likes

Bump, still looking for this, just need a simple formula.

Choose a random direction to shake in (Try Random:NextUnitVector()), afterwards, multiply it by that PlaybackLoudness modifier to make the shake bigger for louder sounds. Experiment with how often you do this. Obviously with camera being scriptable (unless you have some way to shake default camera).

Yes I figured out my own formula, but my problem is the camera gets locked, is there a way to make it still follow player whilst shaking?
This is how I did it.

camera.CFrame = MoveCameraScale * CFrame.Angles(0, 0, math.rad(math.random(-ShakeLoudness, PlaybackLoudness) * 0.0030)) + Vector3.new(math.rad(math.random(-ShakeLoudness * 0.001, ShakeLoudness * 0.001) * 0.030), math.rad(math.random(-ShakeLoudness * 0.001, ShakeLoudness * 0.001) * 0.025), math.rad(math.random(-ShakeLoudness * 0.001, ShakeLoudness * 0.001) * 0.030));

There is a CameraOffset property of humanoid that you could try setting every so often. Be aware its a Vector3 offset.

Well that sucks, is there a way to convert my formula to Vector by any chance?

Honestly, just throw out the rotation and see how it looks with offset. If you need rotation I believe you will need to do custom stuff, which would not be fun.

1 Like

Do I use ToOrientation or Position Property of the cframe formula?

Update on this, I used ToOrientation, but the offset isn’t updating at all, why? i removed MoveCameraScale since it just updates the camera position to where the player’s mouse is located.
it all prints 0

local cframe =  CFrame.Angles(0, 0, math.rad(math.random(-ShakeLoudness, PlaybackLoudness) * 0.0030)) + Vector3.new(math.rad(math.random(-ShakeLoudness * 0.001, ShakeLoudness * 0.001) * 0.030), math.rad(math.random(-ShakeLoudness * 0.001, ShakeLoudness * 0.001) * 0.025), math.rad(math.random(-ShakeLoudness * 0.001, ShakeLoudness * 0.001) * 0.030));
local rx, ry, rz = cframe:ToOrientation()
local dx, dy, dz = math.deg(rx), math.deg(ry), math.deg(rz)
print(rx,ry,rz)
print(dx,dy,dz)
Humanoid.CameraOffset = Vector3.new(dx, dy, dz)

Not sure why you are using angles / orientation here.
Try something like this:

local r = Random.new()
while task.wait(0.1) do 
humanoid.CameraOffset = r:NextUnitVector() * sound.PlaybackLoudness
end

This is an extreme version to show that it should work.

OffSet is still not updating regardless.

Oh my god I just realized I broke the loop WAY TOO EARLY, Oh my god, It’s 4am I should go to sleep.

while Child.IsPlaying do
			task.wait()
			soundPlayback(Child)
			if Child.TimePosition <= 41 then
				soundPlayback(Child, true)
				break
			end
		end

Also yeah theToOrientation method worked but its thrusting instead of vibration but it works anyway!

1 Like

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