Space Shooter Challenge: Camera Shake

Adding some stronger feedback when the Player takes damage, not only feels more immersive, but acts as a great distraction to create both panic and focus. Let’s add it to the Space Shooter!Rather than manipulating the Camera within the Player Script, I…


This content originally appeared on Level Up Coding - Medium and was authored by Calum Slee

Adding some stronger feedback when the Player takes damage, not only feels more immersive, but acts as a great distraction to create both panic and focus. Let’s add it to the Space Shooter!

Rather than manipulating the Camera within the Player Script, I added a CameraShake Script to the Camera object. On experimenting with moving the transform position of the camera, I also decided I didn’t want the background overlay to appear to change positions, so I made it a child object of the camera, now when I shake the camera, the background will follow the camera’s position.

In the Camera Shake Script, I needed a public method so I can call it from the Player Script when damage is taken. This public method simply starts a Coroutine.

First, I need a reference to the default position, so it can be returned to after shaking. This is just a Vector3 variable set to the transform.position.

Then I want a shake length that can be changed in the Inspector, for now I will set it to 0.3f.

Lastly I can create a float for shake time, which is equal to Time.time (the current time) plus the shake length.

Now I could create a while loop that runs while Time.time is less than the Shake Time, so in the current instance, for 0.3 seconds.

In this loop, I want to set the transform position to a random position each frame. Randomising each element of the Vector3 ends up quite long, so instead I created a return type method to get a Random Number.

Back in the while loop I created two floats, one for X, and one for Y, then assigned them to the RandomNumber method. Afterwards, I could pass these two variables in to the new Vector3.

Once the while loop is finished, I want to return the position back to the default.


Space Shooter Challenge: Camera Shake was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Calum Slee


Print Share Comment Cite Upload Translate Updates
APA

Calum Slee | Sciencx (2021-06-08T15:55:26+00:00) Space Shooter Challenge: Camera Shake. Retrieved from https://www.scien.cx/2021/06/08/space-shooter-challenge-camera-shake/

MLA
" » Space Shooter Challenge: Camera Shake." Calum Slee | Sciencx - Tuesday June 8, 2021, https://www.scien.cx/2021/06/08/space-shooter-challenge-camera-shake/
HARVARD
Calum Slee | Sciencx Tuesday June 8, 2021 » Space Shooter Challenge: Camera Shake., viewed ,<https://www.scien.cx/2021/06/08/space-shooter-challenge-camera-shake/>
VANCOUVER
Calum Slee | Sciencx - » Space Shooter Challenge: Camera Shake. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/08/space-shooter-challenge-camera-shake/
CHICAGO
" » Space Shooter Challenge: Camera Shake." Calum Slee | Sciencx - Accessed . https://www.scien.cx/2021/06/08/space-shooter-challenge-camera-shake/
IEEE
" » Space Shooter Challenge: Camera Shake." Calum Slee | Sciencx [Online]. Available: https://www.scien.cx/2021/06/08/space-shooter-challenge-camera-shake/. [Accessed: ]
rf:citation
» Space Shooter Challenge: Camera Shake | Calum Slee | Sciencx | https://www.scien.cx/2021/06/08/space-shooter-challenge-camera-shake/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.