Now, if you’ve read my previous post [link], you might remember I briefly talked about components in context of the inspector. These components are what make up any object in the game, aptly named “GameObject”. In this post, I’m going to go over the most common components you’ll encounter when making a game in Unity.
The gameObject can be seen like a container, a way of grouping together components, into a single object that you’ll find in the hierarchy. GameObjects are the base building blocks for scenes in Unity. The components that it groups together are responsible for how it looks, and behaves.
If you want to follow along, open an empty scene in unity, right click in the hierarchy and add a
[3D Object → Cube].
We’ll keep it simple for now. Double click the cube in the hierarchy to focus on it in the game view.
A transform is an example of one of those components. The transform component is responsible for keeping track of the gameObjects position, rotation and scale in the scene.
Let’s play around with these values to see what they’re for. Double click the cube you added earlier, in the hierarchy, to focus on it. You’ll notice in the inspector window, that it has 4 components, as well as a material, but let’s just focus on the first one for now: The Transform component.
There’s 3 properties: Position, Rotation and Scale. Next to each of those properties you’ll see an x, y and z value. These 3 values are what make up a vector, and each one corresponds to an axis (left/right, forward/backward, up/down).
When hovering over the X, Y or Z, you’ll notice the mouse change into a slider pointer. You can drag left and right to change the values, or manually type a value if you like.
For the position, there’s no limit to it’s values. When each value is at 0, the object will be located at the world origin. This is the absolute middle of the scene. The default cube is 1 unit big in each direction, think of that unit as 1 meter. When you increase or decrease any of the position values by 1, it will move by that much in the scene view.
The rotation on the other hand is expressed in radians, and angle of 90 radians is, as you likely know, a right angle. You’ll notice that if you start increasing the value on any of the 3 directions, it will rotate clockwise when looking at it from that direction. For example, when looking at it from the top down (which would be a negative Y axis), it will rotate clockwise when increasing the value, and counterclockwise when decreasing it.
Lastly, the scale vector, is a vector responsible for scaling an object on each of the axis. It is pretty self-explanatory, but I would caution against relying on this too much. It is very useful when prototyping a scene, but I would advise to keep it at 1 as much as possible. The reason for this is that textures can get skewed when an object is scaled, and gravity (or any physics) might look weird when scales are off. For example: a body would appear to fall very slowly under normal gravity, when everything, including that body, is scaled up a lot. We’ll look at an example in the next topic.
At this moment, unless you’ve added it, the cube has no physics interactions in your scene yet. That’s what the rigidbody is for. A rigidbody is a component that gives your object physical properties, such as weight, drag and angular drag, etc…
Lets add the rigidbody component to the cube and see what it does.
Firstly, in the inspector, with the cube selected in the hierarchy, click add component. You’ll see a little menu pop up (yes, there’s lots of components to play with). Navigate to
[Physics → Rigidbody].
You’ll notice that a component has been added to the 4 components that were already there, with the properties I mentioned earlier. We can play around with these properties, but first, let’s add a terrain to the hierarchy, so that it has something to interact with.
Right click on an empty space in the hierarchy, and navigate to
[3D Object → Terrain].
Now select the cube in the hierarchy, and use the Transform component to move it somewhere above the Terrain you just added.
Hit the play button above the scene view. You’ll see the window change to the game window, and the cube drops to the terrain (unless you’ve changed its properties that is). The reason for this behavior is because the “Use Gravity” checkbox is ticked.
If you now hit the play button again, the cube moves back to the position you put it in, and any values you might have changed while the game was running will be reverted.
Try the same thing with “Use Gravity” unchecked. You’ll see that it doesn’t move this time. The
“Is Kinematic” checkbox is very similar, except that a kinematic body will not interact with any forces, whereas “Use Gravity” only applies to gravity. You can play around with these 2 check-boxes to see how they interact with the terrain while the game is running. Simply hit play, change the check-boxes and move the cube around. You’ll notice that if “Is Kinematic” is enabled, you can move the cube through the terrain, while if only “Use Gravity” is checked, it will struggle to be moved through it. This is because of the next topic we’ll discuss.
A collider is a component that forms a physical boundary on an object. Whenever an object has a collider, The boundary of that collider will repel another collider when they come in contact, just like when 2 real objects collide with each other, they will bounce off.
You’ll notice that the cube you have, already has a Box collider attached. When you disable the Box collider, the cube will fall through the terrain, as well as when you enable the “Is Trigger” checkbox. This latter checkbox is useful later on, if you want objects to interact with each other when they come in contact, without the physical forces at play.
There’s many different types of collider. For instance, the box collider that’s currently on the Cube will give you collisions as if it were a box, but you could add a Capsule collider, or a Sphere collider if you want it to roll around as well.
Another interesting collider, is the wheel collider, which does much more than just handle physics interactions. This specific collider also makes it possible of adding torque, and steering, but that’s for another post.
Meshes and Materials
Now that we’ve looked into some of the components that make up the physical behavior, let’s have a look at visuals. The cube you added earlier has 2 such components by default: the “Mesh Filter“, and the “Mesh Renderer“.
The function of the mesh filter is to display the shape of the object in the scene. In this case, it is a cube, but by clicking the little circle to the right of it, you can change that. It’s also possible to drag and drop any mesh from the project browser tab, to that field.
The mesh renderer is a bit more complex, and deals with the specifics of how to render the mesh to the camera. For now, the most important aspect is the material.
By default, the cube will have a white material. Let’s change that.
In the project tab, right click an empty space, and go to
[Create → Material].
You’ll see that a new item is added, which you can name however you want, and the properties of the material are displayed in the inspector. Click the “albedo” value, and change it to a color you like.
When you changed the material’s properties, simply select the Cube again, and drag and drop the new material to the materials element of the mesh renderer component. You can layer multiple materials if you like, but generally one is enough.
For now, the basic components that I described are enough to play around with Unity and get to know the workflow and build an physical environment to continue to play in. But there’s one more thing that I would like to discuss that will allow you to make the world interactive: the monobehaviour.
A monobehaviour isn’t really a component in itself, but it is the base for a scripted component that you can use on any object. By default, this scripted component will run on each frame, and execute any code you like.
Let’s have a brief look at how to make one, and what it looks like, and if it looks intimidating, don’t worry. My next post will go into detail and explain the basics of how to start writing some of your own scripts.
To add one, go to the inspector window with your cube selected. Click the Add Component button.
There’s 2 ways of adding a component by the way, you can either select it through the menu’s, or simply start typing its name when adding a component through the context menu. When you want to add a script, make sure it does not have the same name as any other component. Let’s start by clicking the “new script” option at the bottom. This will allow you to type in a name for the script. I’ll call mine “CubeController”, because I’ll use it to control the cube.
You’ll notice that besides the component that was added in the inspector, a file was added to the project called “CubeController.cs”. If you select that file, you’ll see it’s code in the inspector window, and if you double click it, it will open it in an IDE.
An IDE, or integrated development environment, is a program used by software developers to develop software. If you’re on Windows, I would advise you to use Visual studio community. It’s free and very powerful. If you’re on Mac, I would advise Visual studio for Mac, or Jetbrains Rider.
On linux, your options are a bit limited at the time, but I would still advise finding the old Monodevelop packages, or trying out Jetbrains Rider. VSCode will also work, but is a bit more difficult when it comes to auto-completion and debugging. But we’ll dive deeper into the coding part in the next post, for now, just play around a bit with Unity, and if you like, see if you can already code something yourself.