Reinforcement learning with Unity Part 3.2 — Making a cube and moving it around

Josh Yang
6 min readSep 30, 2021

Hi everyone, welcome back to the third part (part 2) of my journey to implementing Reinforcement learning using Unity. In this article we are going to continue from the last part where we made a cube and a plane, and we will finally code it up! Let’s get started!

Quick recap…

We made a cube and made it sit nicely on a plane.

Let’s code it up!

There are two ways we can start coding.

  1. We can create a .cs script and write the script from scratch.
  2. We can create a .cs script from Unity and start from a template.

*We are definitely going with the second option.*

Create a script

On the bottom pane, click “Project” tab. Right click on the “Assets” folder select “Create” -> “Folder” and name the folder “Scripts”.

Double click the “Scripts” folder and it will show on the right hand side to the folder tree view that the folder is empty. Right click on there and select “Create” -> “C# Script” and name it “MoveCube”.

Creating Scripts folder and our first script

Now, double click on the “MoveCube” script and it will open up VS Code. Let’s examine the code together.

Unity Code Structure

In Unity scripts almost all classes are derived from a base class call “MonoBehaviour”. If you are familiar with coding in general I am sure you know what this means. I will not go into what a class is and so on but I will quickly explain the concept in regards to our application in Unity.

Quick Explanation: Almost every script we will write will be a class and almost all of them will be derived from a base class called MonoBehaviour (written and defined by the Unity guys). The Monobehaviour base class has a numerous functions (or also called methods) that interact with the Unity environment and by ‘deriving’ from this base class we can also use those functions in our class too.

So back to the code structure. The two most important functions that are derived from MonoBehaviour base class (that concerns us at the moment) are the following:

  1. Start() — this function is called once on the very first frame when the script is enabled and just before the Update() function is called.
  2. Update() — this function is called once every frame.

*There are many other functions and in later articles we will be using other functions too. If you want to take a look at the functions in the Monobehaviour class then check out the documentation.

Also, at the top of the script we can declare that we will be “using” certain functions from the named libraries by writing “using” followed by the name of the library.

So let’s check out our script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveCube: MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
} // Update is called once per frame
void Update()
{
}
}

Here we can see that Unity has created a nice template for us with everything we have talked about!

Let’s verify those functions!

Change the contents of the script to the following.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveCube: MonoBehaviour
{
// initialise frame count with 1
private int frameCount = 1;
// Start is called before the first frame update
void Start()
{
// print "we are starting" message just once on start
Debug.Log("We are Starting...!");
}
// Update is called once per frame
void Update()
{
// print frame number every frame
Debug.Log($"Update Frame Count: {frameCount}");
// increment frame count by 1
frameCount++;
}
}

So what we are doing here is:

  1. Print “We are Starting…!” when we press play and the script is loaded.
  2. Print the frame number every frame.

Very Important!

So now if you go back to the Unity editor and press the “play” button at the top, you will see that Unity will display a game view but nothing is happening — no messages printing anywhere. That is because we haven’t attached the script to an object! In Unity, you must attach a script to an object if you want the script to be loaded and does what you intend to do.

So let’s go and attach our “MoveCube” script to our Cube object. On the bottom pane, click “Project” tab. Navigate to the folder where MoveCube script is (it should already be there since we opened the script through here). Drag and drop the script to the Cube object on the left “Hierarchy” pane. Alternatively, you can click on the Cube object and you can drag and drop the script on the “Inspector” pane on the right hand side.

Now, if you press play again and on the bottom pane click on “Console” tab you should be able to see the messages printing.

Attach a script to the Cube

Let’s make the cube move around

So now we have the understanding of the basic structure, we are going to move relatively faster so we can control the cube with our arrow keys!

Here is the full script with comments so most of the lines should be very clear and easy to understand. But I will still go through what I think is very important underneath.

MoveCube.cs

Important points:

  1. When we want to use scripts or components that are attached to the object we must define and instantiate them using the command “GetComponent<NameOfTheComponentOrScript>()” in our sctipt and for this example, we are doing this in our Start() method.
  2. Input.GetAxisRaw() gives 0 when we don’t press anything, 1 when we press “up” and -1 when we press “down” keys.
  3. Our coordinate system dictates that x-axis is our left and right — right is positive and left is negative value to be specific. Forward and backward is along the z-axis — forward is positive and backward is negative.
  4. We put the inputs into a “Vector3” format where 3D coordinate data points are represented as a single vector object as (x, y, z). We then multiply this vector by a scalar value determined by “force” so we can control how much to scale up or down the resulting force we are going to apply to our cube.
  5. We then pass the resulting force to apply to our cube to the built-in function in our “Rigidbody” component.

So now if you save the script and go back to Unity editor (it will automatically re-load the changes made in the script) and press play button on the top toolbar — you should be able to move the cube around!

Moving the cube!

That’s it!

We have finally managed to make the cube move! You can see that the movement of the cube is a rolling around instead of sliding. This is due to the fact that Unity is simulating friction.

So in the next article we will quickly go over how to change these physics properties as well as giving our cube a colour!

In the mean time you could also give it a try with a sphere or any other shaped object and see how they behave. Also don’t forget to play around with the force multiplier too!

Thank you for reading this article and hopefully this was helpful!

Footnote*

I have made a public git repository so if for any reason you are not seeing what I am seeing you can simply clone the repository and compare!

--

--