How to do it...

To create a camera shake effect, follow these steps:

  1. Create an empty game object in the Scene View and name it CameraRig.
  2. Parent the Main Camera to the CameraRig.
  3. Select the Main Camera and add an Animator component to it.
  4. Open the Animation View.
  5. Create a new Animation Clip and call it CamNormal. The camera should have no motion in this clip. Add keys for both the camera's position and rotation.
  6. Create another Animation Clip and call it CameraShake. Animate the camera's rotation and position to create a shake effect. The animation should have about 0.5 seconds.
  7. Open the automatically created Main Camera controller.
  8. Add a Shake Trigger parameter.
  9. Create two transitions: 
    • CamNormalCameraShake with the condition: Shake Trigger parameter. Has Exit Time should be set to false and Transition Duration set to 0.2 seconds.
    • CameraShake| CamNormal with no conditions: Has Exit Time should be set to true and Transition Duration set to 0.2 seconds.
  10. Write a new script and call it CamShake.cs.
  11. In this script's Update() function, we check if player pressed the space bar. If so, we trigger the Shake Trigger in our controller:
        if (Input.GetKeyDown(KeyCode.Space)) 

        { 

            anim.SetTrigger("Shake"); 
        } 
  1. As always, the anim variable holds the reference to the Animator component and is set in the Start() function with the GetComponent<Animator>() method.
  2. Assign the script to the Main Camera.
  3. Play the game and press the space bar to see the effect.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.90.182