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.
For this recipe, we will use the same package as the previous recipe, BallGame
in the 1362_11_02
folder.
To implement slow motion, follow these steps:
BallGame
package into your project and, from the Project view, open the level named BallGame_01
.ball
in the Prefabs
folder, and save the scene.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); } }
EnergySlider
.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.
Some suggestions for you to improve your slow motion effect even further are as follows.
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;
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.
3.17.154.139