- Unity 3d Fundamentals: Project Demo
- Unity 3d Fundamental Concepts
- Lesson 1: Working with the Unity Terrain
- Lesson 2: Create the First Person Weapon
- Lesson 3: Prefabs and Unity 3d Physics
- Lesson 4: Shooting with Unity 3d raycasting
- Lesson 5: Particle Systems and Audio
- Lesson 6: Creating the Targets
- Video Add-ons: Fixing the Player Weapon
- Lesson 7: Introducing Unity 3d Coroutines
- Lesson 8: Finishing the Environment
- Lesson 9: The Stage Manager
- Lesson 10: The Game Manager
- Lesson 11: The UI Manager (Unity 3d GUI)
- Lesson 12: The Main Menu
In this lesson we will import the assets to create the initial area and add some physics in our scene using the Unity 3d Physics System.
Import the assets from the Asset Store
The first thing we need to do is to import all the necessary assets from the Asset Store. Here you have the full list of the required assets:
Build the Initial Area
We will start by creating the walls of our initial area. Since all walls are equal we will use a modular approach by creating a prefab for a single wall and then creating several instances to make the full cage.
What is a Prefab?
A Prefab is a template for a GameObject. It allows us to store a GameObject with Components and Properties already set. We can even store a full hierarchy of GameObjects, all with their own Components and default values.
One example you already know is the First Person Controller.
When to use Prefabs?
Prefabs are great for storing objects that will be reused several times in our scene. Instead of duplicating each individual object we should always make a prefab and then create prefab instances.
Why should we use Prefabs?
When we just duplicate each individual object all copies are independent. So when all copies are already in place, if we want to modify anything we need to manually change each copy.
With a Prefab this process is automatic. We are able to just modify the Prefab and all instances will be automatically updated because they maintain a connection to the original Prefab and will reflect all changes that are applied.
And back to our scene…
We will first position all objects to create a single wall section. A Prefab always needs a singular object at the root. It is not possible to create a Prefab with a selection of GameObjects. If you have several objects just create an empty GameObject to be the root of your hierarchy and add all others as Childs.
With all objects in place we will create a Prefab with the topmost object of our hierarchy.
Prefabs Instances appear blue in the Hierarchy meaning that they are connected to a Prefab Template. On the Inspector you also have three buttons related to the Prefab Connection:
- Select: Select the original Prefab;
- Revert: Revert any changes made to this object to the original Prefab;
- Apply: Apply the changes made to this object to the original Prefab;
With the new Prefab we can duplicate our instance to create the rest of the cage. We may also create a root object to organize the hierarchy view.
Now we need to close our cage. We are going to use the Wooden Board models on the Construction Site Pack.
The Wooden Board 1 has the right shape to fit in our cage. However, when importing the scale is too small.
Instead of scaling the model on our scene we are going to change the Scale Factor on the Import Settings of our mesh. Following the Prefab logic, it is preferable to change the scale factor of the mesh instead of manually scaling each copy on the scene.
Now that the model has the right size we need to adapt our cage walls to fit perfectly with the board.
Add the barrels or any other model you want. Remember to adjust the Scale Factor of each model and to check the Generate Colliders option. The Barrel package already has three different textures and materials. To apply a different material to you barrel just drag the Material into your object or select a new one using the Browse option on the Mesh Renderer Component.
Also place the weapon inside the Mixing Barrel and adjust the Trigger position.
Add some Physics to your scene
Let’s make this more interesting by adding some Physics to our cage door. First we will use the Physics system to make the door rotate on its Y axis. To make any object use the physics system we need to add a Rigidbody component. So let’s start by adding this component to our door.
The Rigidbody puts the object under the control of the physics system. So it will automatically respond to gravity – if the Use Gravity is enabled – and to the collision of other colliders.
So if we enter Play Mode you will see that the object will fall into the ground.
Now we want our door to rotate around its Y axis. We need another Physics Component called the Hinge Joint. This component constrains the rigidbody to move like if it’s connected by a hinge just like a real door. Add a new Hinge Joint to our door.
When you add the Hinge Joint you will see a small orange arrow appear next to your model. This is where the hinge was applied – by default to the X axis – and the rigidbody movement will be constrained to rotate around this axis. Give it a try.
Our door is in fact rotating around an axis but it’s on the wrong axis. We need to change it to the Y axis and also adjust the hinge position to the left of the door.
Now our door is rotating on the right axis but I also want to limit the door rotation to a 180 degree angle instead of being completely free. You can do this by enabling the Use Limits flag on our Hing Joint component and defining the angles to limit.
But the idea is to have the door locked at the beginning so we will add a new plank behind the door to hold it in place and prevent it from rotating. Just add a new plank or duplicate the current door to create the lock.
The lock is already working but we want to remove it during the game by shooting it. We could just make the plank disappear when it’s shot but I want the plank to fall into the ground so we need to also put the plank under physics control. Add a Rigidbody to the plank.
The problem is that the plank will now automatically fall into the ground because it’s responding to gravity. You see that we cannot enable/disable the Rigidbody just like any other component. But it has a property called Is Kinematic. This property makes the Rigidbody stop responding to the physics engine and only to forces or transform changes done by scripting. So let’s enable this property and later we will disable it when the plank is shot.
Names to keep
In the next lesson we will make our weapon being able to shoot stuff.