Refactoring the Camera Shake in Unity2D
In my last article, I showed how to make the camera shake when the player takes damage, without using any plugins. One thing I don’t like about my current camera shake setup, is that it’s running in void Update, meaning, the game is checking every frame to see if the camera should be shaking or not. That can be a costly operation, so I would prefer to have a public method that can run until it’s completion outside of void Update, and then go back to an idle state.
To get started, I comment out my previous code implementation so I don’t have any conflicts. Once I get my new implementation working, I will remove the old method completely.
I will now be passing in the variables for shake duration and magnitude, locally through the camera shake method, so I no longer need to reference them as variables at the top of my CameraShake class.
My previous GetComponent and null check remain untouched.