Section 5- Triggers, Materials, and PreFabs



32) Make a "fall catcher."
Create a new plane and position it some distance below the ground.
Make sure to scale it to be large enough to always catch the Player falling off the edge.

Mark its mesh collider component as "Is Trigger." (Now it doesn't actually block objects, but just sends a "trigger event" when touched.)

Remove the mesh renderer component. (We don't want to actually see this object.)

Rename the plane to "FallCatcher" and put it in an organized place in the hierarchy


33) Tag the fall catcher.
We need to "tag" the FallCatcher so it can be easily identified later in script.

Select FallCatcher and then select Tag->Add Tag in the Inspector to show the Tag Manager

Expand the Tags array and make a new tag named "FallCatcher" in the Element 0 slot.


34) Check for fall catcher triggers in the PlayerMovement script
Write an OnDeath function than that handles what happen the player "dies." (Reset its position.)

Add the OnTriggerEnter function to the script. (It takes in a pointer to the Collider that caused the trigger as a parameter)
Check if the tag property of the passed collider is equal to "FallCatcher." If so call the OnDeath function.

Updated C# PlayerMovement example: here.


35) Make a deadly mine.
Make another sphere that will act as a static "enemy" that kills the Player on contact

Mark its sphere collider component as "Is Trigger."

Give it a tag of "Mine" in the same way we tagged the FallCatcher


36) Make a mine material.
Select "Assets->Create->Material" from the top menu bar to make a new material.

Click the new material's color selector box in the upper-right corner of the Inspector and choose a dangerous color!

Name the new material "MineMaterial" in the Project view.

Select the Mine, then select its renderer component, and expand materials.
Click the small circle to the right of the Element 0 material to set it to our new dangerous material.


37) Make the mine a prefab and make the whole level dangerous!
To make a prefab, simply drag an object into the project view. It will show up as an asset there and the object will turn blue in the inspector.

Duplicate the mine in the Inspector with right click->Duplicate of ctrl+D as normal. (The duplicates will also be blue.)

Put mines everywhere!

Select any mine and change a property such as setting "Cast Shadows" to false.
Then click "Apply" in the new prefab bar at the top of the Inspector to apply the change to all prefabs of that type.
That's the power of pre-fabs!





Prev: Section 4- Level Building, More Scripting, and Publishing
Next: Section 6- Sound and GUI

Back to Main