Creating a Player Object and Adding Simple Player Movement in Unity
Setting Player Position and Speed
Setting up the Scene
The first thing I like to do is set up my scene view.
- First, select the main camera in the “Hierarchy” panel.
- In the “Inspector” panel, click the box next to the“Clear Flags” label and select “Solid Color” from the drop down menu.
- Click the box next to the “Background” label and choose a color for your background. I chose black for this particular project.
Creating the Player
The next step will be to add a player to the scene.
- Right click in the “Hierarchy” panel, hover over “3D Object”, and select “Cube”.
This will add a cube to your scene.
Note: You can also add an object to your scene by clicking the + (PLUS) symbol just under the "Hierarchy" tab.
- Move the cube down, close to the bottom of the scene.
Creating the Material
Next, lets add some color to your Player object. This is accomplished by creating a material and attaching that material to the Player object.
- First, right click on “Assets”, select “Create”, select “Folder”, and name it “Materials”.
- Right Click on the “Materials” folder, select “Create”, select “Material”, and name it “Player_mat”.
- With the new material selected, click on the color box in the “Inspector” panel and select a color of choice.
Adding Material to Player Object
There is more than one correct way to add material to an object in Unity. Below is a list of 3 ways to accomplish this.
- Click and drag the material from the “Project” panel and drop it onto the Player object in the Scene View.
- Click and drag the material from the "Project" panel and drop it onto the Player object in the "Hierarchy" panel.
- Select “Player” in the “Hierarchy” panel. Click and drag the material into the “Materials” Element slot on the “Inspector” panel.
Note: Only 1 method above is required to add material to an object. My personal preference is number 2 above (click and drag material onto the "Player" object in the "Hierarchy" panel).
Adding the Player Script to the Player
To be able to manipulate an object’s movement, we use scripts. You will have to first create a C# Script.
- Right click on the “Assets” folder in the “Project” panel, select “Create”, select “Folder”, and then rename the folder to “Scripts”.
- Right click on the “Scripts” folder, select “Create”, select “C# Script”, and rename the file to “Player”.
Note: If you don't name your script before pressing enter, you will have to change the name of the class by going into the script. It's best practice to always check to make sure the script name matches the class name inside the script.
To access the script, simply double click the script within the “Scripts” folder.
Now that the script is open, we can begin to add our own code.
Note: It’s always good practice to write pseudocode and comments into the script. Pseudocode is written as comments inside the script. It details what your code is supposed to do.
Inside the Start() method, add a comment/pseudocode.
Type:
// Take the current position and assign a new position (x,y,z)
Note: To assign a new position, we use the "=" symbol.
current position = new position
This pseudocode is saying exactly what is written. You are wanting to get the current position of the Player object and assign a new position to the Player object.
- Attach the “Player” script to the Player object. Confirm it is attached by selecting the “Player” object in the “Hierarchy” panel and checking that it is attached in the “Inspector” panel.
Setting New Position of the Player
To set the new position of the Player object to (0,0,0) on the (x,y,z) axes, we have to access the Transform component on the Player object. The Transform component holds the current position, rotation, and scale of the object. We can do this with code in the “Player” script.
- Inside the Start() method of your “Player” script, type:
transform.position = new Vector3(0, 0, 0);
'transform' is used to access the transform of the object to which
the script is attached'.' is the dot operator, which takes you further into the transform'position' is a level deeper into the transform and holds the
position of the object (x,y,z)If you want to go further into the transform position, you can add one of the coordinates. For example:"transform.position.x"
"transform.position.y"
"transform.position.z"
- Be sure to save your “Player” script.
Note: It is always good practice to save your work frequently.
Now your Player object will move instantly to the (0, 0, 0) position upon running the game.
Moving the Player Across the Screen
You’ve already moved the Player object into a new position with a quick snap. Now you will move the Player object across the screen to the right via the “Player” script. To do this, we will have to access the Transform of the Player object as we did before.
To accomplish a move across the screen we have to use one of the Transform public methods called Translate. Translate moves the Transform in the direction and distance of translation.
- Inside the Update() method of your “Player” script, type:
- // new Vector3(1, 0, 0)
transform.Translate(Vector3.right); - Save your “Player” script.
Note: If you were to put the Vector3(1, 0, 0) as your Translate parameters, this means move the Vector3 1 position to the right. If you use a negative 1 (-1, 0, 0), this is telling it to move the Vector3 1 position to the left. Using the .right and left has the same affect and is easier to write.
So, transform.Translate(Vector3.right) is the same as transform.Translate(Vector3(1, 0, 0)).
This alone will have a different affect than what you are wanting. Save the “Player” script and run the game. After your previous code snaps the Player object to (0, 0, 0), this new code will move the Player object to the right of the screen at supersonic speed. You will notice in the “Inspector” panel, the Transform X position continues to rapidly increase. This is because the code is updating every frame.
So as long as the game is running, the Update() method is being called. The only code in the Update() method is the code to move the Player object to the right. So the Player object will continue to move further to the right unless we add some more code to our “Player” script.
Slowing Down the Player Object
To slow the Player object down, we will have to manipulate speed at which the Player object moves. This is done with “Time.deltaTime”.
“Time.deltaTime” will set the conversion process to real time. It can be thought of as 1 second.
- In the Update() method, change:
transform.Translate(Vector3.right); to
transform.Translate(Vector3.right * Time.deltaTime); - Save your “Player” script.
Run the program. You will notice this moves the Player object 1 unit/sec.
If you want to change the rate of speed (i.e.; 5 units/sec), you would simply multiply the Vector3 by 5, then multiply by Time.deltaTime.
- Go back to your “Player” script and change
transform.Translate(Vector3.right * Time.deltaTime); to
transform.Translate(Vector3.right * 5 * Time.deltaTime); - Save your “Player” script.
Now when you run the program, you will notice the Player object moves at 5 units/sec.
And there you have it. You‘ve created a Player object and added simple movement to it.