Nerd For Tech
Published in

Nerd For Tech

Tip of the Day: Simple 2D Camera Shake in Unity

One of the best ways that increases player immersion in the game is a Camera Shake.

Camera shakes are especially useful for explosions, when player takes damage, or for collectibles.

If you are not using Cinemachine, here is a simple way to do it.

Start by creating a C# script call it CameraShake (or whatever you want) and attach it to your main camera.

//This script will only hold the function that you are going to call when needed from other scripts// Create an IEnumerator that takes two variables
// A variable to control the duration of the shake
// Another variable to control the magnitude of the shake
// Start the method by declaring a new Vector3 originalPos to determine the camera's starting position
//Declare a float for elapsedTime and set it to 0f
//For the shake: best do it in a while loop as long as the elapsedTime is less than the duration of the shake//In the while loop, set two floats, one for the xOffset and one for the yOffset and multiply each by the magnitude (it is good to use a random range for each offset// Set the localPosition of the camera based on these two offsets
// finish the loop by incrementing the elapsedTime with Time.deltaTime
//Finally after the loop is done, set the localPosition to the originalPos and close the IEnumerator method

Use this script to call the shake whenever you need. If you are up to the challenge then try writing the code from the above Pseudocode.

Here is the final result

Camera shakes when destroying the asteroid or when taking damage

One final note, to achieve best results when the main camera sets back to its original position you need to add child the camera in its own parent game object

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Recommended from Medium

Getting Started With Raspberry Pi

NUMPY IN PYTHON

DApp open dual version update [issue 42] (2019.02.25)

Creating a monorepo from separate repos (merging repositories)

Blazing fast IPC with gRPC ( Part 1 )

How to code in Python: good and bad practices of coding in Python

Integration of CRM into Twilio Flex

Custom status code matcher for API testing in Python with PyHamcrest

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mohamed Hijazi

Mohamed Hijazi

A knowledge seeking biologist who is following his passion into a full time career in Unity / Game Development. https://www.linkedin.com/in/mohamed-hijazi/

More from Medium

Mastering Unity Scripting | Summary — Chapter 2: Debugging

Journal 143 — Unity Project Development, 2.5D Shooter

Unity Features 101: Assembly Definitions

Making nice cameras in Unity with the Cinemachine plugin