From reading the other posts, it can be seen that player feedback is a pretty important thing in my mind. It adds to the experience and provides instant confirmation on things happening. The player shouldn't need to look at the number of gas stations remaining to see if they did indeed go through it or if it was a miss. There should be both visual and audible feedback to tell the player.
So in my game, when a player hits an obstacle, the screen shakes. The camera is in a train and makes no sense what so ever for it to do so in a real world situation. But this is a game and sometimes doing things that would for certain never happen in the real world, makes the game world more believable.
The screen shake isn't seen at its full potential in the above gif as the frame rate is too low. In-game though, the camera jitters along the x and y axis for a short period of time. So how does this work?
Well when the camera is called to shake the screen it requires the sender to provide 3 values. A duration, an amount and an intensity.
- Duration is the total time the screen shake will take place for.
- Amount is the distance between the shake points.
- A low number would make the shake small.
- A high number would make the shake large.
- Intensity is the speed at which the camera moves to each of the shake points.
- A low number would make the shake slower.
- A high number would make the shake more violent.
So how does the screen actually shake? What's the code behind it?
- First we get the initial camera's position and make that the original position.
- This will be used once the shake has ended.
- Then we set the target position:
- targetPos = originalPos + (Random.insideUnitSphere * amount);
- This returns a value in a spherical radius around the camera.
- Then we simply lerp towards the target pos at the speed of the intensity.
- Once we're at that, repeat the above steps and stop once the time elapsed is greater than the duration.
This is one way to do camera shake. I know that i've been using this same system for my games in the past and it works well. Of course there are many other ways to it but this is the way I know best and it's very customisable.