In this tutorial, I’m going to teach you how to move a simple cube in Unity. The first thing we need to do is create a Unity project.
From Unity Hub, click the Left Mouse Button (LMB) on the blue “NEW” button.
Enter the project name and the location where you want the project saved and use the LMB to click the blue “CREATE” button.
It only takes a few moments for Unity to create our project. Our project already contains two objects, the Main Camera, and a Directional Light. We are going to add a “Cube” to the project.
We do this by clicking the left mouse button on “GameObject”, then moving our mouse down to the 3D object label; then over to “Cube”. Click the LMB to add a “Cube.
Our newly added “Cube” shows up in the “Hierarchy” tab under the “Directional Light”. Let's rename the “Cube” to “Player”.
We do this in the “Inspector” tab. Hover over the word “Cube” and click the LMB, backspace 4 times and then type “Player”, then “Enter”.
“Player” now appears below the “Directional Light” in the “Hierarchy” tab. In the “Hierarchy” tab, select the “Main Camera” with the LMB.
In the “Inspector” tab select “Skybox” with the LMB, then select “Solid Color”. Use the LMB to click the “Background Color” and select a light color.
Place the mouse pointer within the “Project” and press the Right Mouse Button (RMB) and select “Folder”. Type in “Scripts” for the name of the folder. From within the “Project” tab, press the RMB again and select “C# script”, type in the name “Player”. Now we need to add our “Player C# script” to our “Player” object (the cube). We do this by dragging the C# “Player script” from the Project tab up to the “Hierarchy” tab and dropping it on “Player”.
We can see our new “Player (Script) has been added at the bottom of the “Inspector” tab.
Move the mouse pointer to “#Player” and double click with the LMB. This will open Visual Studio.
We will start writing code in the “void Start() function.” Type in the following code:
The “C# code: transform.position = new Vector3(0,0,0)” allows us to set the “X”, “Y”, “Z” positions of the “Transform” tab from within our C# script. By placing this code in the “void Start()” function our “Player” position will be set to “0” on the “X”, “Y”, and “Z” axis. Press “Ctrl+s” to save. Return to the Unity Editor window so we can test our code.
And it works. ..
In part 2, we will add to our C# program so we can use the “W, A, S, D” keys to move our player on the “X” and “Y” axis.