Implementing slow motion

Since Remedy Entertainment's Max Payne, slow motion, or bullet time, became a popular feature in games. For example, Criterion's Burnout series has successfully explored the slow motion effect in the racing genre. In this recipe, we will implement a slow motion effect triggered by the pressing of the mouse's right button.

Getting ready

For this recipe, we will use the same package as the previous recipe, BallGame in the 1362_11_02 folder.

How to do it...

To implement slow motion, follow these steps:

  1. Import the BallGame package into your project and, from the Project view, open the level named BallGame_01.
  2. In the Inspector, create a new tag Ball, apply this tag to prefab ball in the Prefabs folder, and save the scene.
  3. Add the following C# script BulletTime to First Person Controller:
    using UnityEngine;
    using UnityEngine.UI;
    using System.Collections;
    
    public class BulletTime : MonoBehaviour
    {
        public float sloSpeed = 0.1f;
        public float totalTime = 10f;
        public float recoveryRate = 0.5f;
        public Slider EnergyBar;
        private float elapsed = 0f;
        private bool isSlow = false;
    
        void Update ()
        {
    
            if (Input.GetButtonDown ("Fire2") && elapsed < totalTime)
                SetSpeed (sloSpeed);
    
            if (Input.GetButtonUp ("Fire2"))
                SetSpeed (1f);
    
            if (isSlow) {
                elapsed += Time.deltaTime / sloSpeed;
                if (elapsed >= totalTime) {
                    SetSpeed (1f);
                }
    
            } else {
                elapsed -= Time.deltaTime * recoveryRate;
                elapsed = Mathf.Clamp (elapsed, 0, totalTime);
            }
            float remainingTime = (totalTime - elapsed) / totalTime;
            EnergyBar.value = remainingTime;
        }
    
        private void SetSpeed (float speed)
        {
            Time.timeScale = speed;
            Time.fixedDeltaTime = 0.02f * speed;
            isSlow = !(speed >= 1.0f);
        }
    }
  4. From the Hierarchy view, use the Create drop-down menu to add a Slider to the UI (Create | UI | Slider). Please note that it will be created as a child of the preexisting Canvas object. Rename it EnergySlider.
  5. Select EnergySlider and, from the Inspector view, Rect Transform component, set its position as follows: Left: 0; Pos Y: 0; Pos Z: 0; Right: 0; Height: 50. Then, expand the Anchors settings and change it to: Min X: 0; Y: 1; Max X: 0.5; Y: 1; Pivot X: 0; Y: 1, as shown in the following screenshot:
    How to do it...
  6. Also select the Handle Slide Area child and disable it from the Inspector view, as shown in the following screenshot:
    How to do it...
  7. Finally, select the First Person Controller from the Hierarchy view, find the Bullet Time component, and drag the EnergySlider from the Hierarchy view into its Energy Bar slot, as shown in the next screenshot:
    How to do it...
  8. Play your game. You should be able to activate slow motion by holding down the right mouse button (or whatever alternative you have set for Input axis Fire2). The slider will act as a progress bar that slowly shrinks, indicating the remaining bullet time you have.

How it works...

Basically, all we need to do to have the slow motion effect is decrease the Time.timeScale variable. In our script, we do that by using the sloSpeed variable. Please note that we also need to adjust the Time.fixedDeltaTime variable, updating the physics simulation of our game.

In order to make the experience more challenging, we have also implemented a sort of energy bar to indicate how much bullet time the player has left (the initial value is given, in seconds, by the totalTime variable). Whenever the player is not using bullet time, he has his quota filled according to the recoveryRate variable.

Regarding the GUI slider, we have used the Rect Transform settings to place it on the top-left corner and set its dimensions to half of the screen's width and 50 pixels tall. Also, we have hidden the handle slide area to make it more similar to a traditional energy bar. Finally, instead of allowing direct interaction from the player with the slider, we have used the BulletTime script to change the slider's value.

There's more...

Some suggestions for you to improve your slow motion effect even further are as follows.

Customizing the slider

Don't forget that you can personalize the slider's appearance by creating your own sprites, or even by changing the slider's fill color based on the slider's value. Try adding the following lines of code to the end of the Update function:

GameObject fill = GameObject.Find("Fill").gameObject;
Color sliderColor = Color.Lerp(Color.red, Color.green, remainingTime);
fill.GetComponent<Image> ().color = sliderColor;

Adding Motion Blur

Motion Blur is an image effect frequently identified with slow motion. Once attached to the camera, it could be enabled or disabled depending on the speed float value. For more information on the Motion Blur image effect, refer to http://docs.unity3d.com/Manual/script-MotionBlur.html.

Creating sonic ambience

Max Payne famously used a strong, heavy heartbeat sound as sonic ambience. You could also try lowering the sound effects volume to convey the character focus when in slow motion. Plus, using audio filters on the camera could be an interesting option.

See also

Refer to the recipe Pausing the game in this chapter for more information.

..................Content has been hidden....................

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