Acceleration in C# – Unity Game Development Tutorial

“Acceleration is pressing his eyeballs out of their right shape. High tech astigmatism.”
― James S.A. Corey, Drive

In this free tutorial by @MammothCompany: learn fundamental C# skills to implement acceleration in a Unity game!

In this tutorial:

Acceleration is a term used to define the change in velocity. In other words, acceleration is how quickly an object speeds up or slows down.

Part 1: Setup

To follow along with this tutorial, you’re going to need a Unity game open with a player and a velocity. If you don’t have this yet, use our previous tutorial to set up exactly that!

Part 2: Defining Acceleration with C#

In your Player script, Player.cs, create a float variable named acceleration. This variable will represent the player’s acceleration.

Give the variable the initial value 2.5f. 

public float acceleration = 2.5f;

With this code, we have implemented that, with each second of the game, the player’s speed will increase by 2.5 until it reaches a maximum speed of 4, the value of movementSpeed.

Part 3: Defining Speed

Next, declare and initialize a private float variable named speed. Assign this variable to contain Player’s initial speed, 0.

In the update method, define the velocity on the X axis to equal the value of the speed variable.

void Update () {
GetComponent<Rigidbody> ().velocity = new Vector3(

speed,
GetComponent<Rigidbody> ().velocity.y,
GetComponent<Rigidbody> ().velocity.z

);
}

Let’s test the acceleration and speed variables we just created!

Save the script, and open Unity. Press Play.

The cube will fall. It will not move to the right because its speed is currently 0.

Let’s fix that!

Part 4: Using Acceleration to Change Speed

We will use acceleration to increase speed until speed’s value reaches movementSpeed. In Update, use the following code to add the value of acceleration to speed every second of the game.

void Update () {
speed = speed + acceleration;

GetComponent<Rigidbody> ().velocity = new Vector3(

speed,
GetComponent<Rigidbody> ().velocity.y,
GetComponent<Rigidbody> ().velocity.z

);
}

Multiply acceleration by Time.deltaTime so that the speed changes at the same rate regardless of the processing speed of your user’s device.

speed = speed + acceleration * Time.deltaTime;

Part 5: Using If Statements

Next we need to ensure that speed will never be greater than movementSpeed. Otherwise, the acceleration will keep increasing the speed to infinity.

You can use an if block to implement a condition.

An if block runs code if a certain condition passes. Use the following format to set up an if block in the Update method.

speed = speed + acceleration * Time.deltaTime;

if () {

}

The if block’s condition goes in between the parentheses. If the condition passes, the code between the curly brackets gets executed.

For our game, the code that needs to be executed is that speed should be limited to movementSpeed. Set this up within the curly brackets of the if block.

WIthin the parentheses of hte if block, set up the condition required for that code to run. For our game, the condition is when speed surpasses movementSpeed.

In our case, the condition is that With the following code, when speed surpasses movementSpeed, set speed to equal movementSpeed.

if (speed > movementSpeed) {
speed = movementSpeed;
}

Part 6: Testing Acceleration

Let’s test this code to see if it is functioning properly!

Save the script, and open Unity. Press Play. The player will move slowly at first, but its speed will increase. Press Stop. Feel free to change the value of Acceleration to a different value, such as 4. 

Note: You can increase the X Scale value of Plane so that the player does not fall off the floor.

Review

—Team Mammoth from Mammoth Interactive INC. Tutorial by Glauco Pires and Transcribing by Alexandra Kropova

Liked this tutorial? Get a free game development video course!

More resources on this topic:

Leave a Reply

Your email address will not be published. Required fields are marked *