Lesson 2.4 - Collision Decisions

Tutorial

·

Beginner

·

+10XP

·

50 mins

·

(2011)

Unity Technologies

Lesson 2.4 - Collision Decisions

Overview:

Our game is coming along nicely, but there are are some critical things we must add before it’s finished. First off, instead of pressing S to spawn the animals, we will spawn them on a timer so that they appear every few seconds. Next we will add colliders to all of our prefabs and make it so launching a projectile into an animal will destroy it. Finally, we will display a “Game Over” message if any animals make it past the player.

Project Outcome:

The animals will spawn on a timed interval and walk down the screen, triggering a “Game Over” message if they make it past the player. If the player hits them with a projectile to feed them, they will be destroyed.

Languages available:

Overview Video

1. Make a new method to spawn animals

Our Spawn Manager is looking good, but we’re still pressing S to make it work! If we want the game to spawn animals automatically, we need to start by writing our very first custom function.

  1. In SpawnManager.cs, create a new void SpawnRandomAnimal() {} function beneath Update()
  2. Cut and paste the code from the if-then statement to the new function
  3. Call SpawnRandomAnimal(); if S is pressed

2. Spawn the animals at timed intervals

We’ve stored the spawn code in a custom function, but we’re still pressing S! We need to spawn the animals on a timer, so they randomly appear every few seconds.

  1. In Start(), use InvokeRepeating to spawn the animals based on an interval, then test.
  2. Remove the if-then statement that tests for S being pressed
  3. Declare new private startDelay and spawnInterval variables then playtest and tweak variable values

3. Add collider and trigger components

Animals spawn perfectly and the player can fire projectiles at them, but nothing happens when the two collide! If we want the projectiles and animals to be destroyed on collision, we need to give them some familiar components - “colliders.”

  1. Double-click on one of the animal prefabs, then Add Component > Box Collider.
  2. Make sure “Auto Save” is enabled in the upper right corner of the Scene view.
  3. Click Edit Collider, then drag the collider handles to encompass the object.
  4. Check the “Is Trigger” checkbox.
  5. Use the Overrides dropdown to apply this Trigger to all of the prefabs for this animal.
  6. Repeat this process for each of the animals and the projectile.
  7. Add a RigidBody component to the projectile and uncheck “use gravity”.

4. Destroy objects on collision

Now that the animals and the projectile have Box Colliders with triggers, we need to code a new script in order to destroy them on impact.

  1. Create a new DetectCollisions.cs script, add it to each animal prefab, then open it
  2. Before the final } add OnTriggerEnter function using autocomplete
  3. In OnTriggerEnter, put Destroy(gameObject);, then test
  4. In OnTriggerEnter, put Destroy(other.gameObject);

5. Trigger a “Game Over” message

The player can defend their field against animals for as long as they wish, but we should let them know when they’ve lost with a “Game Over” message if any animals get past the player.

  1. In DestroyOutOfBounds.cs, in the else-if condition that checks if the animals reach the bottom of the screen, add a Game Over messsage:
    Debug.Log(“Game Over!”)
  2. Clean up your code with comments
  3. If using Visual Studio, Click Edit > Advanced > Format document to fix any indentation issues
    (On a Mac, click Edit > Format > Format Document)

6. Lesson Recap

New Functionality

  • Animals spawn on a timed interval and walk down the screen
  • When animals get past the player, it triggers a “Game Over” message
  • If a projectile collides with an animal, both objects are removed

New Concepts & Skills

  • Create custom methods/functions
  • InvokeRepeating() to repeat code
  • Colliders and Triggers
  • Override functions
  • Log Debug messages to console

Complete this tutorial