Unity Tutorial Part 3: Components
In the final part of our Unity tutorial series, you’ll learn how to make your first game in Unity with C# from scratch: a twin-stick shooter called Bobblehead Wars! By Brian Moakley.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Unity Tutorial Part 3: Components
40 mins
This is an excerpt taken from Chapter 1, “Hello Unity” of our book Unity Games by Tutorials, newly updated for Unity 2018.1, which walks you through creating four Unity games from scratch — and even shows you how to develop for VR in Unity. Enjoy!
Welcome to the third and final part of this Unity mini-series! In this tutorial, you’ll learn all about Components in Unity while you give your hero free reign to blast the landscape with bullets!
This tutorial continues on from the previous tutorial, Unity Games, Part 2: GameObjects.
At this point, you’ve accomplished your goal of having all the main actors ready, but it’s essentially an empty stage. Sure, you might win some avant-garde awards for a poignant play on the lack of free will, but that’s not going to pay the bills.
In this tutorial, you’ll add some interactivity to your game through the use of Components, which are fundamental to Unity game development. If you were to think of a GameObject as a noun, then a component would be a verb, or the part that performs the action; a component does something on behalf of a GameObject.
You’ve learned a bit about components already. In the previous two parts in this tutorial series, you learned how each GameObject has one required component: The Transform component, which stores the GameObject’s position, rotation and scale.
But Unity comes with far more components than that. For instance, there’s a light component that will illuminate anything near it. There’s an audio source component that will produce sound. There’s even a particle system component that will generate some impressive visual effects. And, of course, if there isn’t a component that does what you want, you can write your own.
In this tutorial, you’ll learn about several types of components, including the Rigidbody component, the script component, and more. By the end of this tutorial, your marine will be running and gunning!
Getting Started
If you completed the last tutorial, open your current Bobblehead Wars project to pick up where you left off. If you got stuck or skipped ahead, download the materials for this course using the links at the top or bottom of this page, open the Bobblehead Wars starter project from this tutorial’s resources.
Currently, the space marine only knows how to stand like a statue. He needs to move around if he’s going to avoid being some alien’s snack.
The first thing you’ll add is a Rigidbody component, which opts the GameObject into the physics engine. By adding it, you’ll enable the GameObject to collide with other GameObjects.
Adding the Rigidbody Component
In the Hierarchy, select the SpaceMarine GameObject and click the Add Component button in the Inspector.
You’ll see many different categories. When you know which component you need, simply search by name. Otherwise, select one of the categories and pick the best option.
Click the Physics category then select Rigidbody.
You’ll see that a Rigidbody component was attached to the GameObject. Congratulations! You’ve added your first component.
Each component has its own set of properties, values and so forth. These properties can be changed in the Inspector or in code. Components also have their own icons to make it easy to determine their type at a glance.
You’ll notice some icons in the top right-hand corner of each component, like this:
The first icon is the Reference icon. Click it. It’ll open another window with the documentation page for that component. If you installed the documentation, this page is on your computer — and, yes, the search bar works.
The second icon is a new feature in Unity 2018. This is the presets button. As you customize your components, you may prefer a certain, preset, configuration created previously.
In the Rigidbody component, check the IsKinematic checkbox. Also, uncheck the Use Gravity option. You’ll learn about those options in a moment. But once you have unchecked them, click the presets button.
You can save that configuration as a preset, then switch to it as needed. You can also create new GameObjects with those assigned presets, saving you time.
This dialog will list all of your presets. Click the Save current to and from the Project Browser, select the Presets folder. Call it Kinematic. Now when you click the preset button again, you’ll see your newly saved preset. Presets are a great way to save your current configuration settings when you want to make changes.
The last of the three buttons in the top right-hand corner of the component is a gear icon. Click it. This dialog will appear:
Here are the most important options listed here:
- Reset will reset the component to its default values.
- Move to Front and Move to Back adjust the ordering of sprites in 2D games.
- Remove Component will delete the component from the GameObject — you can undo this action.
- Copy Component allows you to copy a component from one GameObject and paste it onto another.
- Paste Component as New will paste a copied component to a GameObject.
- Paste Component Values allows you to overwrite the values of the current component from a copied component. Specifically, you can copy the values of a component while your game is being played. When you stop the game, you can paste those values onto another component. This is quite useful because sometimes it’s useful to tweak things as you play to see what works in practice.
From the menu, select remove component. Now click the add component button again, and add a Rigidbody component back to the GameObject. Remember, it’s in the Physics category. Finally, click the presets button and select the Kinematic preset.
Notice that the Rigidbody component was auto-magically updated per your preset. But you may be asking, what’s a Rigidbody?
By adding the Rigidbody component to the space marine, you’ve made it so that he now can respond to collision events and respond to gravity. However, you don’t want him bouncing off enemies or walls. You definitely want to know about those collisions events, but you don’t want the hero to fly out of control just because he bumped into a column.
The isKinematic
property tells the physics engine that you’re manually controlling the marine, rather than letting the physics engine move the marine for you. But the physics engine is still aware of where the marine is and when it collides with something. This is helpful so that when the marine collides with an alien, the physics engine will notify you so you can make something happen — like the space marine getting chomped!
By unchecking the Use Gravity
option, the space marine won’t be affected by gravity. Again — you don’t need this because you’ll be moving the marine manually and are only using the Rigidbody for collision detection.
Now comes the fun part: It’s time to make that marine dance! Or move. That’s fine, too.
Unity provides some built-in components to make movement easy. But for learning purposes, in this tutorial. you’ll do everything manually through the power of scripting.