Camera Shake

Matthew Bartosh
2 min readJul 11, 2023

Today my objective is to create additional feedback to the player when they’re hit by adding a camera shake.

Relatively easy process here, create a camera shake script for the main camera. I’ll include floats for duration, shake amount, and the factor it decreases by, as I’m going to have one that slowly decreases instead of suddenly starting and stopping.

This is what the code looks like. I’ll save the original position, and the original shake duration, then when the method is called, it turns _shakeTrue = true, which will check if shake duration is above 0, if it is, it’ll move the camera position to a random position within a sphere, then it’ll decrease the duration by the factor. Once _shakeDuration gets to below 0, _shakeTrue will become false and it’ll set position and duration back to the originals.

The most difficulty I had during this process was calling the method. I had to utilize SendMessage(“ShakeCamera”) instead of calling the script through the camera object. But it works fine, giving me this wonderful result!

--

--