Home / Legacy / Lesson 3: Prefabs and Unity 3d Physics

Lesson 3: Prefabs and Unity 3d Physics

/
/
/
1151 Views
This entry is part 5 of 15 in the series Unity 3d Fundamentals Course

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.

Lesson 3: Prefabs and Unity 3d Physics

Lesson Outline

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:

Lesson 3: Prefabs and Unity 3d Physics

1.1 Woven Wood Fence Construction Kit by Unity Technologies

Lesson 3: Prefabs and Unity 3d Physics

1.2 Shanty Town: Barbed Wire by Unity Technologies

Lesson 3: Prefabs and Unity 3d Physics1.3 Construction Site Pack by Alex McDonnell

Lesson 3: Prefabs and Unity 3d Physics1.4 Mixing Barrel by Unity Technologies

Lesson 3: Prefabs and Unity 3d Physics1.5 Barrel by Universal Image

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?

Announce: Check here for more on Prefabs.

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.

Lesson 3: Prefabs and Unity 3d Physics

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.

A Prefab will always update its instances. However it is possible to modify an individual instance without changing the original Prefab.

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.

Lesson 3: Prefabs and Unity 3d Physics

When all objects are in place you may want to center your root on the object hierarchy. Select the root and use the command Center on Child on the GameObject Menu.

With all objects in place we will create a Prefab with the topmost object of our hierarchy.

Lesson 3: Prefabs and Unity 3d Physics Lesson 3: Prefabs and Unity 3d Physics Lesson 3: Prefabs and Unity 3d Physics

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;

Lesson 3: Prefabs and Unity 3d Physics

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.

Lesson 3: Prefabs and Unity 3d Physics

Now we need to close our cage. We are going to use the Wooden Board models on the Construction Site Pack.

Lesson 3: Prefabs and Unity 3d Physics

The Wooden Board 1 has the right shape to fit in our cage. However, when importing the scale is too small.

Lesson 3: Prefabs and Unity 3d Physics

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.

Lesson 3: Prefabs and Unity 3d Physics

Now that the model has the right size we need to adapt our cage walls to fit perfectly with the board.

Lesson 3: Prefabs and Unity 3d Physics

Another option on the Import Settings that we should check is the Generate Colliders. When enabled a Mesh Collider component is automatically applied to our GameObject. However, in some situations we won’t need a Collider or maybe we may need to replace the Mesh Collider by a more basic one.

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.

Lesson 3: Prefabs and Unity 3d Physics Lesson 3: Prefabs and Unity 3d Physics

Also place the weapon inside the Mixing Barrel and adjust the Trigger position.

Lesson 3: Prefabs and Unity 3d Physics

 

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.

Lesson 3: Prefabs and Unity 3d Physics

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.

Lesson 3: Prefabs and Unity 3d Physics

So if we enter Play Mode you will see that the object will fall into the ground.

I will change the collider of my door to a Box Collider. If you want to use the Mesh Collider with the Rigidbody check the Convex property of the Collider, otherwise the object will not detect the collision with the ground and just fall through it.

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.

Lesson 3: Prefabs and Unity 3d Physics

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.

Unfortunately the Character Controller is not able to interact with physics objects by itself. But unity has a working script on the documentation: check here. Create a new script, paste the code and apply it to the First Person Controller object. Don’t forget to match the name of the script file and the class.

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.

Lesson 3: Prefabs and Unity 3d Physics

I will just adjust the door position between two fence sticks so it looks more believable.

Lesson 3: Prefabs and Unity 3d Physics

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.

Lesson 3: Prefabs and Unity 3d Physics

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.

Lesson 3: Prefabs and Unity 3d Physics

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.

Lesson 3: Prefabs and Unity 3d Physics

 

Names to keep

What’s next…

In the next lesson we will make our weapon being able to shoot stuff.

 

Series Navigation<< Lesson 2: Create the First Person WeaponLesson 4: Shooting with Unity 3d raycasting >>

2 Comments

Leave a Comment

Your email address will not be published. Required fields are marked *