Now that we have Unity opened up, we can actually start building our project. To get started, let's build a player that will always move forward. Let's start with that now:
- Let's create some ground for our player to walk on. To do that, let's go to the top menu and select GameObject | 3D Object | Cube.
- From there, let's move over to the Inspector window and change the name of the object to Floor. Then, on the Transform component, set the Position to (0, 0, 0), which we can either type in, or we can right-click on the Transform component and then select the Reset Position option.
- Then, we will set the Scale to (7, 0.1, 10):
In Unity, by default, 1 unit of space in Unity is representative of 1 meter in real life. This will make the floor longer than it is wide (X and Z), and we have some size on the ground (Y), so the player will collide and land on it because we have a Box Collider component attached to it.
- Next, we will create our player, which will be a sphere. To do this, we will go to Game Object | 3D Object | Sphere.
- Rename the sphere to Player and set the Transform component's Position to (0, 1, -4):
This will place the ball slightly above the ground, and shifts it back to near the starting point. Note that the camera object (you can see a camera icon to point it out) is pointing toward the ball by default because it is positioned at 0, 1, -10.
- We want the ball to move, so we will need to tell the physics engine that we want to have this object react to forces, so we will need to add a Rigidbody component. To do so, go to the menu and select Component | Physics | Rigidbody. To see what happens now, let's click on the Play button that can be seen in the middle of the top toolbar:
As you can see in the preceding screenshot, you should see the ball fall down onto the ground when we play the game.
- Click on the Play button again to turn the game off and go back to the Scene tab, if it doesn't happen automatically.
We want to have the player move, so in order to do that, we will create our own piece of functionality in a script, effectively creating our own custom component in the process.
- To create a script, we will go to the Project window and select Create | Folder on the top-left corner of the menu. From there, we'll name this folder Scripts. It's always a good idea to organize our projects, so this will help with that.
- Double-click on the folder to enter it, and now you can create a script by going to Create | C# Script and renaming this to PlayerBehaviour (no spaces).
- Double-click on the script to open up the script editor (IDE) of your choice and add the following code to it:
using UnityEngine;
public class PlayerBehaviour : MonoBehaviour
{
// A reference to the Rigidbody component
private Rigidbody rb;
// How fast the ball moves left/right
public float dodgeSpeed = 5;
// How fast the ball moves forwards automatically
public float rollSpeed = 5;
// Use this for initialization
void Start ()
{
// Get access to our Rigidbody component
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update ()
{
// Check if we're moving to the side
var horizontalSpeed = Input.GetAxis("Horizontal") * dodgeSpeed;
rb.AddForce(horizontalSpeed, 0, rollSpeed);
}
}
In the preceding code, we have a couple of variables that we will be working with. The rb variable is a reference to the game object's Rigidbody component that we added previously. It gives us the ability to make the object move, which we will use in the Update function. We also have two variables dodgeSpeed and rollSpeed, which dictates how quickly the player will move when moving left/right, or when moving forward, respectively.
Since our object has only one Rigidbody component, we assign rb once in the Start function, which is called when the game starts, as long as the game object attached to this script is enabled.
Then, we use the Update function to check whether our player is pressing keys to move left or right as based on Unity's Input Manager system. By default, the Input.GetAxis function will return to us a negative value moving to -1 if we press A or the left arrow. If we press the right arrow or D, we will get a positive value up to 1 returned to us, and the input will move toward a 0 if nothing is pressed. We then multiply this by dodgeSpeed in order to increase the speed so that it is easier to be seen.
Finally, once we have that value, we will apply a force to our ball's horizontalSpeed units on the X-axis and rollSpeed in the Z-axis.
- Save your script, and return to Unity.
- We will now need to assign this script to our player by selecting the Player object in the Hierarchy window, and then in the Inspector window, drag and drop the PlayerBehaviour script from the Project window on top of the Player object. If all goes well, we should see the script appear on our object, as follows:
Note that when writing scripts if we declare a variable as public, it will show up in the Inspector window for us to be able to set it. We typically set a variable as public when we want designers to tweak the values for gameplay purposes.
- Save your scene by going to File | Save Scene. Create a new folder called Scenes and save your scene as Gameplay. Afterward, play the game and use the left and right arrows to see the player moving according to your input, but no matter what, moving forward by default: