Slow Motion Effect - Unity Game Development Tutorial
We've got a ball and a stack of boxes that all have a Rigidbody component attached so that they react to forces and collisions.
We then have the following simple script attached to the ball that applies a force when the arrow keys are pressed.
using UnityEngine; public class BallMovement : MonoBehaviour { public float forceSize; private Vector3 forceDirection; private Rigidbody rigidbody; void Start() { rigidbody = GetComponent<Rigidbody>(); } void Update() { float horizontalInput = Input.GetAxis("Horizontal"); float verticalInput = Input.GetAxis("Vertical"); forceDirection = new Vector3(horizontalInput, 0, verticalInput); forceDirection.Normalize(); } private void FixedUpdate() { Vector3 force = forceDirection * forceSize; rigidbody.AddForce(force); } }
We're going to add a new script to the ball to apply a slow motion effect when the space bar is held down. To do this we'll select the ball in the hierarchy, click 'Add Component', and add a new script called 'SlowMotion'
We'll double click the script to open it in Visual Studio, and change the script to match the following
using UnityEngine; public class SlowMotion : MonoBehaviour { public float slowMotionTimescale; private float startTimescale; private float startFixedDeltaTime; void Start() { startTimescale = Time.timeScale; startFixedDeltaTime = Time.fixedDeltaTime; } void Update() { if (Input.GetKeyDown(KeyCode.Space)) { StartSlowMotion(); } if (Input.GetKeyUp(KeyCode.Space)) { StopSlowMotion(); } } private void StartSlowMotion() { Time.timeScale = slowMotionTimescale; Time.fixedDeltaTime = startFixedDeltaTime * slowMotionTimescale; } private void StopSlowMotion() { Time.timeScale = startTimescale; Time.fixedDeltaTime = startFixedDeltaTime; } }
We've added a public variable to hold the scale we want to apply when the space bar is held down. A scale of 1 will result in the game running at normal speed. A value of 0.5 will result in the game running at half speed.
When the space bar is released we want the game to return to normal speed, so we've created a private variable to hold the original time scale.
It's also recommended to scale the fixed delta time value when changing the time scale so we've also added a private variable for storing the original value of this.
In the Start method we're then setting the value of the two private variables.
In the Update method we're checking if the space bar has been pressed. If it has, we're calling a method to start the slow motion effect.
We're then checking if the space bar has been released. If it has, we're calling a method to stop the slow motion effect.
In the method to start the slow motion effect, we're setting the time scale to the desired slow motion scale. We're also scaling the fixed delta time accordingly.
In the method to stop the slow motion effect, we're setting the time scale and the fixed delta time back to the original values.
Let's save the script and switch back to Unity.
We'll set the slow motion time scale to 0.1 in the Inspector.
We can now move the ball with the arrow keys and when we hold down the space bar we'll get this cool slow motion effect.
When we release the space bar the game will return to normal speed.
We can play around with the scale value to create different effects. For example, let's set the scale value to 2.
When we press the space bar now, the game will run at double speed.
That covers everything for this tutorial. We hope that you found it useful. Please leave any questions or feedback in the comments below, and don't forget to subscribe to get notified when we publish our next post.
Thanks.
Comments
Post a Comment