Using Physics Collisions for Interactions
Outline
This lab will focus on the use of the physic engine and how using the collision data to trigger different
events in the game. We are going to add some obstacles which will use different methods to trigger
events.
Tutorial
1. Begin by making a copy of your previous lab code for Angry Penguins. Copy the entire folder to a
new location as this will be the starting point for this lab. Make sure that the project is running
correctly before progressing.
2. The first form of interaction will be from an obstacle which will be animated and attempt to
block the penguins from hitting the boxes. Create a new Sphere GameObject.
3. Name it "BlockingObstacle".
4. Check and ensure that the object is on the same axis point at which the penguins will be
spawned. In this case it is 0 on the Z axis.
5. We are going to add animations to this object so that it moves and affects the penguins when
they are launched towards the blocks. Create a new animation on the BlockingObstacle and
name it "BlockingObstacle_VerticlaMovement".
6. Create a simple up and down wave motion using the animation panel. It should look something
like the image below. You may want to change the speed and the curves to change how it
moves.
7. You will want to set this animation to loop during the game. On the Project panel select the
animation file, BlockingObstacle_VerticalMovement, and on the inspector panel change the
wrap mode to Loop. You can also try PingPong to reverse the animation.
8. Test the game and make sure that penguins collide with the new object while the animation is
playing.
9. Now we need to collect the collision data from the object when it collides with the penguin.
Create a new C Sharp script called "BlockingObstacle". Add the following code.
10. Add this script to the BlockingObstacle in the scene.
11. Run the game.
12. You should receive the following error:
In our script we defined a public Game Object and try to get a component from this object during the
Start method. Since we added the object to the blocker object we also need to assign a Game Object to
this property. In the inspector panel you should see that no Game Object is added to property.
Drag the Game Object which contains the game controller component from the Hierachy panel into this
property.
13. We are going to use a trigger now to slow or stop the speed of the Blocking Obstacles
animation. Create a capsule object and place it in the scene like so.
14. Name it "Pill".
15. Create a new material to colour the pill like the following and apply it to the Game Object.
16. On the Capsule Collider component of the Pill object set the Is Trigger property to true by
enabling the checkbox. This will cause the object to become passable and non physical in the
scene.
17. Create a new C Sharp script, name it Pill. This will hold the trigger functions we need to call
when the penguin passes through the object.
18. Add the Pill script to the Pill Game Object.
19. Add the BlockingObstacle Game Object in the scene to Pill components Blocker Object property.
20. Play the game. Test the trigger is working by launching the penguin towards it. When it is
triggered you should notice that the animation of the blocker will slow down.
Tasks
To become more familiar with the physics trigger system try and complete the following.
1. Create a new Game Object which will increment the players score while still touching the
trigger. Place a limit on how much the player can score by using an numeric property and
decrementing its value each time the trigger is called.
2. When a penguin collides with the blocking object give it an animation to play. A simple such as
changing the penguins colour to flash red or yellow. This should be called when the penguin is
always colliding. Note: make sure not to keyframe the penguins transform component in the
animation as this will change its position in the game.
Figure 1 - Animating the penguins material to flash to red then back
The Unity3D reference for the runtime libraries outlines the other methods to use in place of
OnTriggerEnter.
Figure 2 - MonoBehaviour runtime class methods in the Unity3D documentation