Unity Tutorial: Rocket Jumping in Overwatch

Jeffrey Su
10 min readMar 31, 2017

--

The playable Overwatch hero ‘Pharah’ can use a unique form of jumping called Rocket Jumping using her Jump Jets giving her great aerial control in the virtual plane. In this tutorial we will attempt to recreate this Rocket Jumping movement system through Unity by breaking down and rebuilding the components to this mechanic. This tutorial is designed for beginners by using the simplest methods.

This tutorial will ONLY cover:

  • Creating a basic custom FPS character in Unity.
  • Unity physics.
  • Unity C# coding.
  • Pharah’s ground movement, Rocket Jumping and Hovering.

This tutorial will NOT cover:

  • Ground jumping/crouching of an FPS character in Unity.
  • Usage constraints on Pharah’s Jump Jet and Hover.
  • Sound.
  • Dynamic movement details.

Assumed knowledge:

  • Basic understanding of Unity.
  • Experienced in Unity at a basic level.
  • Experienced in coding at a basic level.
  • Knowledge on how to use the Transformation component in Unity.

Programs used:

  • Unity (Version 5.3.4f1 Personal).
  • MonoDevelop (Version 5.9.6).

Platform:

macOS Sierra (Version 10.12.3).

CONTENT

Setup

Creating the Character

Character Physics

Character Ground Movement

Character Mouse

Rocket Jumping & Hovering

Results

_Setup

We will first need to do some things before beginning the tutorial after the project is created.

  • First let’s begin by setting up a new Unity project (the name of the project is irrelevant). File > New Project
  • For conveniency, ensure that your project layout is the same as the project layout below. If it is different, it can be changed. Window > Layouts > 2 by 3 > Move and change size of windows until they are roughly the same as the project layout below.
  • We will need the Assets Store window. Window > Assets Store
  • Ensure that your scene view is NOT set to 2D. Scene Window > 2D
  • Create 2 folders; Scenes and Scripts, under Assets folder. This will organise our contents (optional). In the Project window: Assets > Right click > Create > Folder
  • Save the current scene into the Scenes folder. File > Save Scene as…
  • There are two Asset Store packages will we want to download and import; Standard Assets which instantly can give us gridded prototyping materials for the environment and Free Guns Pack which is optional but will give us a basic rocket launcher to look at in our FPS camera view for this tutorial. These two packages can be downloaded from the links provided in the RESOURCES section at the bottom of this tutorial. It can also be found in the Assets Store tab in Unity if the package names are searched.
  • Ensure there is a Directional Light in the project. GameObject > Light > Directional Light
  • Ensure there is a Floor to work with. The Standard Assets package contains prototyping floors for us to use. In the Project window: Assets > Standard Assets > Prototyping > Prefabs
  • Resize and then reset the it’s position to the origin and reposition it to a desired coordinate.

The project is now set up and we are ready to begin.

_Creating the Character

For complication reasons with the Unity physics system we will not be adapting upon the FPS character given in the Standard Assets. We will be setting up our own custom FPS character and script.

  • Create a FPS body using a Capsule. GameObject > 3D Object > Capsule
  • Reset the Capsule’s position to the origin and above the floor.
  • Rename your Capsule to ‘Player’ (optional). In the Hierarchy window: Right click our Player > Rename: ‘Player’
  • Attach the Camera (which should already be in the scene) to the FPS body by dragging and dropping onto the Player in the Hierarchy window and reset its transformation to be centred in the body and to a desired height. This will be the FPS camera view of our character and its position will always be in accordance to the position of our Player.
  • We will be using a Field of View value of 70 for our Camera for this tutorial. This can be changed in the Inspector in the Camera component after the Camera is selected.
  • Repeat the attaching step with the RocketLauncher prefab but this time, attach it to the Camera instead of the Player and reposition it to a desired destination. In the Project Window: Assets > Free Guns Pack > Prefabs > RocketLauncher

Your scene and Game view should now look something like this.

_Character Physics

In order for our character to be involved in Unity physics, we need several components to allow it to function correctly.

  • Attach a Rigidbody to it. Select the Player > In the Inspector: Add Component > Physics > Rigidbody
  • Ensure that gravity is ticked in the Rigidbody component for the Player in the inspector. Ensure that Freeze Rotation for X, Y and Z are all ticked in the Rigidbody component for the Player in the Inspector. This will be explained later in the tutorial.
  • If we run the game, the gravitational pull is noticeably weak. There are many ways to increase gravity on a Rigidbody but we will use the easiest method for this tutorial and that is using the built in component ‘Constant Force’. Add a Constant Force component to the Player. Select the Player > In the Inspector: Add Component > Physics > Constant Force
  • Assign a value to the Y axis variable of Force in the Constant Force component for the Player in the inspector. We will be using -9.8 for this tutorial. Ensure that there is a minus sign in front of the the 9.8 or the character will fly up instead of fall down in the game.

This concludes creating the foundation of our Character for us to adapt upon.

_Character Ground Movement

To create a very basic ground movement system for the player we will only need a short script which will be provided further below in consecutive segments and each segment will be explained under the code itself inside the segment boxes. (Note: Due to the layout of Medium, some lines of code will continue to the next which may look visually confusing. However, it will not cause any errors once copied into your script).

  • Firstly, create a new C# script for the Player. Select the Player > In the Inspector: Add Component > New Script > Language: C Sharp > Name: ‘P_Controller’
  • Script beginning:
using UnityEngine;
using System.Collections;

public class P_Controller : MonoBehaviour {

public float p_WalkSpeed;
// This will be the walking speed variable for the Player. By using ‘public’ we can access this from the Inspector.
  • Start:
     void Start () {
Cursor.lockState = CursorLockMode.Locked;
}
// This will lock the mouse within the Game window.
  • Update:
     void Update () {
if (Input.GetKeyDown ("escape")) {
Cursor.lockState = CursorLockMode.None;
}
// If "escape" is pressed the mouse will no longer be locked inside the Game window. Note: 'GetKey' is to hold the key down, 'GetKeyDown' is to press the key. float p_WalkFB = Input.GetAxis ("Vertical") * p_WalkSpeed;// This will create a variable based on the "Vertical" axis in conjunction to the the walking speed variable established earlier. This will allow the Player to move forwards and backwards. float p_WalkLR = Input.GetAxis ("Horizontal") * p_WalkSpeed;// This will create a variable based on the "Horizontal" axis in conjunction to the the walking speed variable established earlier. This will allow the Player to move left and right. p_WalkFB *= Time.deltaTime;
p_WalkLR *= Time.deltaTime;
// This will ensure that the two variables will function in accordance to real time instead of frames. transform.Translate (p_WalkLR, 0, p_WalkFB);
}
}
// This activates everything above so that the Player is able to move. Note: In the brackets, p_WalkLR is the X axis, 0 is the Y axis and p_WalkFB is the Z axis.// IMPORTANT! From before, we froze the X, Y and Z axis rotations. This script only creates transition in a direction which means rotational aspects of the Player body are not part of it. This means, with additional forces on the body (such as bumping into an object) it will cause the body to either fall or roll. By freezing rotations of the Player Rigidbody for all 3 axes, this problem will no longer be present.
  • Your script should now look something like this.
  • Assign a value to the p_WalkSpeed variable in the P_Controller script component for the Player in the inspector. We will use 5 as the walking speed of the player for this tutorial.

_Character Mouse

Again, to create a very basic aiming system for the player we will only need a short script which will be provided further below in consecutive segments and each segment will be explained under the code inside the segment boxes. (Note: Due to the layout of Medium, some lines of code will continue to the next which may look visually confusing. However, it will not cause any errors once copied into your script).

  • Firstly, create a new C# script for the Camera. Select the Camera > In the Inspector: Add Component > New Script > Language: C Sharp > Name: ‘P_Mouse’
  • Script beginning:
using UnityEngine;
using System.Collections;
public class P_Mouse : MonoBehaviour { Vector2 p_MouseLook;
Vector2 p_MouseSmooth;
// This will create script names for two Vector2 components. public float sensitivity;
public float smoothing;
// These will be the mouse sensitivity and smoothing variable for the Player. By using 'public' we can access this from the Inspector. GameObject p_Camera;// This will create a script name for a referenced GameObject.
  • Start:
     void Start () {
p_Camera = this.transform.parent.gameObject;
}
// This will assign the referenced GameObject of p_Camera as the parent GameObject's transformation to this code.
  • Update:
     void Update () {
var cp = new Vector2 (Input.GetAxisRaw ("Mouse X"), Input.GetAxisRaw ("Mouse Y"));
// This will create a variable name for the repositioning of the mouse. A Vector2 has 2 components; an X axis and a Y axis. This Vector2 will be using the "Mouse X" axis (moving left and right) and "Mouse Y" axis (moving up and down). cp = Vector2.Scale (cp, new Vector2 (sensitivity * smoothing, sensitivity * smoothing));// This will assign the values of how much transition happens for Mouse X and Y per frame. p_MouseSmooth.x = Mathf.Lerp (p_MouseSmooth.x, cp.x, 1f / smoothing);
p_MouseSmooth.y = Mathf.Lerp (p_MouseSmooth.y, cp.y, 1f / smoothing);
p_MouseLook += p_MouseSmooth;
// This will smoothen the Camera rotation using the Lerp and the smoothing variable created earlier. transform.localRotation = Quaternion.AngleAxis (-p_MouseLook.y, Vector3.right);
p_Camera.transform.localRotation = Quaternion.AngleAxis (p_MouseLook.x, p_Camera.transform.up);
}
}
// This will activate the rotation of the Camera.
  • Your script should now look something like this.
  • Assign a value to the p_MouseLook and p_MouseSmooth variable in the P_Mouse script component for the Player in the inspector. We will use 5 and 1 respectively for this tutorial.

_Rocket Jumping & Hovering

Creating Pharah’s Rocket Jump and Hovering effect may seem difficult but it can actually be achieved using very simple scripting. This will be provided further below in consecutive segments and each segment will be explained under the code inside the segment boxes. (Note: Due to the layout of Medium, some lines of code will continue to the next which may look visually confusing. However, it will not cause any errors once copied into your script).

  • Firstly, create a new C# script for the Player. Select the Player > In the Inspector: Add Component > New Script > Language: C Sharp > Name: ‘P_Jump’
  • Script beginning:
using UnityEngine;
using System.Collections;

public class P_Jump : MonoBehaviour {

public float p_ForceRJ;
public float p_ForceHover;
// This will create script names for two variables for Rocket Jumping and Hovering. Rigidbody p_Rigidbody;// This will create a script name for a referenced Rigidbody.
  • Start:
     void Start () {
p_Rigidbody = GetComponent<Rigidbody> ();
}
// This will get the Rigidbody component of the GameObject in which this script is attached to for the script name. Note: The Rigidbody of the Player MUST be referenced in order for this script to work.
  • Update:
     void Update () {
if (Input.GetKeyDown (KeyCode.LeftShift)) {
p_Rigidbody.AddForce (0, p_ForceRJ, 0);
}
// If LeftShift is PRESSED, the Player will be propelled vertically only by the force p_ForceRJ. This will allow the Player to Rocket Jump. if (Input.GetKey (KeyCode.Space)) {
p_Rigidbody.AddForce (0, p_ForceHover, 0);
}
}
}
// If Space is HELD DOWN, the Player will experience p_ForceHover/frame vertically upwards. This will allow the Player to Hover.
  • Your script should now look something like this.
  • Assign a value to the p_ForceRJ and p_ForceHover variable in the P_Jump script component for the Player in the inspector. We will use 1000 and 25 respectively for this tutorial.

_Results

This concludes the tutorial. Below is the result (with some prototyping prefabs additionally added for the environment. Not part of this tutorial). If there are any scripting errors, double check your 3 scripts for any grammar mistakes or left out symbols. Eg. Leaving out a semicolon.

--

--