That’s right, by now you can make game objects in Unity, and you know how to use the basic tools to manipulate those objects you’ve created. Today I’m going to show you how to use some basic code to put those objects in motion.
Now, do not fear at the mention of coding, I will teach you everything you need to know to implement this into your project and the tools that will teach you how to experiment with your code.
To make this simple begin with making a cube and placing it where every you fancy. It is a good practice to label every object, to keep things tidy and take out any guess work. For today’s lesson label the cube Player. You can change the name of an object at the top of the inspector view.
As a bonus lesson we are going to change the color of the player. To do this we need to create a material folder in the project view. Right-click in the project view, hover over create and select folder. Label the folder as Materials.
Materials are anything that provides visual representation for a game object.
Right click again in the project view and this time hover over create and click material. Label this new material as Player_mat.
_mat is a naming convention that makes searching for materials easier. When you are working on a large-scale game you will have 1000+ materials
With the Player_mat selected, in the inspector view you will see the albedo channel. Double clicking this will bring up a color wheel and you can now make your player whatever color you wish.
To apply the material, you can simply click and hold the material and drop it on top of the object either in the scene view or in the hierarchy.
you can also change the background color of the game view. Simply select the main camera and in the Inspector view under the clear flags click the drop box and choose single color. Now you can choose the color you wish, for the sake of this tutorial lets go with a nice black for now.
Now we can set the proper aspect ratio. We want 16:9, and the reason for that is that it is the most widely accepted ratio on all platforms. simply go to the top of the game view and click free aspect and select the 16:9 ratio.
Now we are ready to set the players starting position. To do this we are going to need to create a folder in the project view and label it Scenes.
Right click again and hover over create and this time select C# Script. Before you do anything else, while the script is highlighted name it Player. Naming the script before you click off it for the first time will ensure that your class name and file name are the same.
Now to open the player script double click and that will open Microsoft Visual studio. This is where your coding will take place. Inside you will see Player : MonoBehaviour.
MonoBehaviour is a Unity specific term. This allows us to drag and drop scripts onto objects allowing us to control them. MonoBehaviour has two functions by default, void start and void update. these are methods called automatic from Unity.
Void start is the start of the game
Void update is considered a game loop, all logic and player input go in the void update.
We are now ready to set the player’s starting position. In the void start you will type //take the current position = new position (0, 0, 0)
//take the current position, represents the task, and you are assigning it a new position at (x, y, z).
Now to do this we will need to understand that Unity works in a hierarchy structure. First, we need to add the script to the player. You can do this by drag and dropping the script directly onto the player or, if the player is selected you can drop the script into the inspector view.
You will need to access the transform component of the player. To do this you will go back to Visual studio and underneath the first line you wrote you will need to add another line.
transform.position = new vector3(0, 0, 0);
Here is the hierarchy I mentioned before. You start with transform, and inside the transform component you want to access the position. If you needed to specify you could add .x, .y, or .z. If you needed you could also access the rotation or the scale by replacing the position accordingly.
The equals sign is used to set the new position.
Vector3 is what defines all position types of Unity, anything involving positioning of a game object or an object in Unity is going to be reassigned through a Vector3. Every time you use a vector3 you will always use a new vector3.
Make sure you save this script and return to Unity. If you haven’t attached the script to the player, make sure to do so now. Once it is attached you could place your player anywhere in the scene view and as soon as you hit the play button on the top of Unity your player will snap into its starting position.
You’ve done it! You have successfully written your first code. If you did not succeed do not fear, I didn’t get it the first time either. Just go back through the steps and make sure your spelling is correct. Visual studio has a zero-tolerance policy for spelling.
If you would like to end it there, I wouldn’t blame you. You’ve been through so much already. Although, we could go a little further and add motion to this player.
Let’s begin with selecting the player and with your mouse inside the scene view press F to focus the camera on your object. Using the perspective tool in the top right corner of the scene view rotate until you are in the X, Y view.
You will notice that moving the player to the right that the number in the X axis increases, and decreases going left. Similarly, you will see the Y axis increase when you pull the player up and decrease when you go down.
This is a good time to learn about Script API. Script API is a reference manual that has information on Unity functions. This is the tool you will use to teach yourself how to use Unity and even problem solve any issues you may come across. To access this, you click on a circle button with a question mark inside it at the top right of any component in the inspector tool. That will open the corresponding script API.
To see how to use scripts API open it now. At the top click scripting API and type Translate. After the search click on Transform.Translate and you will see a page that list a description of what the formula does and a formula to show you how to type it out. Do not worry about the example, focus on the formula and description.
Now that you’ve read about translate, we need to go back to Visual Studio. This time we are going to be working in the void update. Type out transform.Translate(Vector3.right);
Every line of code ends with a semi-colon. Make sure to save the script and return to Unity. Once you hit play now your player will take off. Did you see how fast he went? If you look in the inspector, you’ll notice that unless you stop it the player will continue to the right off screen.
Let me show you what happened. The part of the code (Vector3.right) is the same as writing (new Vector3(1, 0, 0)); If you wanted the player to go left you could have written .left, or (-1, 0, 0).
If the speed of sound isn’t what you had in mind, then allow me to explain why it’s moving so fast, and how to alter the speed. First things first, 1 unit in Unity is the equivalent of 1 meter in the real world. The void update runs at 60 frames per second, so it is running 60 meters per second. To convert 1 meter per frame to 1 meter per second we need to incorporate real time.
* Time.deltaTime); think of it as one second.
The technical term for Time.deltaTime is really the time in seconds it takes to complete the last frame to the current frame. Meaning if we want to move 5 meters per second, we need to multiply the Time.deltaTime
transform.Translate(Vector3.right * Time.deltaTime);
This works on the distributor property. A math equation, vector3(1, 0, 0) times 5 = (5, 0, 0) * Time.deltaTime. Meaning we have taken 1 meter per second and multiplied it by 5. If you are still wanting to go left the math is the same. Vector3(-1, 0, 0) time 5 = (-5, 0, 0) * Time.deltaTime, negative five meters a second.
There you have it. You now have a few lines of code under your belt, not to mention the access to the script API, you can now manipulate your code adjust it to your liking. With this foundation you are well on your way to making your very own games!