42:Camera Shake!

Objective: Every time the player gets hit shake the screen a little bit.

To start off for shaking the camera, we will need a few variables. We need a Vector3 to hold a reference to the position we want the game to stay at. Next is a float for how long the shaking will last, the current shake duration, and how strong the shake will be. Finally we’ll have a bool to see if it’s time to shake the camera.

In the start function we will set the currentShakeDuration to the shakeDuration. As well as assing the originalPos to the current transform.postion.

In the Update function we will need to check to see if it’s time to shake the camera, to do this we’ll see if the bool isShakeCameraTime is true. If it is check to see if the current duration is greater than zero. With the currentShakeDuration greater than zero, we’ll take the current transform.postion and assign it to the originalpos plus a random unit inside a circle multiplied by the shakeStrength and Time.deltaTime. Random.insideUnitCircle returns value within a circle that has a radius of 1. We’ll also subtract the currentShakeduration by Time.deltaTime.

If the the currentShakeDuration is zero or less, we’ll return the transform.position back to the originalPos. We’ll make the isShakeCameraTime back to false and assign the currentShakeDuration back to the shakeDuration.

Now that the camera will have a little shake, we will need a way to assign a true value to the isShakeCameraTime. We’ll do this by creating a public function and inside it we’ll assign the bool to true.

In the Player script, we need a reference to the CameraShake script.

As well as finding the game object that has it attached and check to see if it’s null.

In the Damage function we will call the public function from the CameraShake script.

So now, every time the player receives damage, the shakeCamera will be called and the camera will move around a little bit. On the main camera, that has the script, you can adjust the values as needed. Higher the number the stronger the shake.

--

--

--

I am passonate on learning to program and use Unity to become a skillful Unity Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to Land a top-notch tech intern- while you are in college.

GEMS — Telegram AMA — January 28

101 Most Important Frequently Asked Core Java Interview Questions to Ace an Interview — Part 2

READ/DOWNLOAD*$ Starting out with Visual C# FULL B

How to create Scrollable Image Horizontal Menu (Html & CSS)

How to run Python, C, CPP script with Nodejs.

How Contract manufacturing companies benefit from modern LIMS software

How to output a list of PostgreSQL databases and tables using psql

How to output a list of PostgreSQL databases and tables using psql

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
Tyler Smallwood

Tyler Smallwood

I am passonate on learning to program and use Unity to become a skillful Unity Developer

More from Medium

Day 111: Introduction to Tilemap in Unity

Platformer: Elevator

Spawn and Destroy😎

Creating a Sci-fi Lab Room