COMP1400 – Programming for Designers

Class announcements for Programming for Designers

COMP1400 – Programming for Designers random header image

Lab 5

Posted by on August 17th, 2010 · Lab

Your task in this lab is to practise using for loops and prefabs.

We’re going to make a simple game in which the player has to click on a bunch of moving targets to destroy them.

  1. Create a target object of your choice. Give it a script to make it move around randomly.
  2. Create a prefab and drag the object into the prefab.
  3. Use the prefab to create multiple copies of the object.
  4. Use the Inspect to change the properties of the prefab and observe how the instances are affected.
  5. Create an empty “joint” object.
  6. Add a script to the joint to create many instances of the prefab when the game is started. Use a variable to specify the number.

Unity has a built-in facility for detecting when the user clicks on an object. To make it work, your object must have a Collider component attached to it. Most of the built-in primitive objects (boxes, spheres, etc) have colliders by default. If you are using a model you downloaded, you will have to add your own collider.

  1. Make sure your prefab includes a collider. If you don’t have one, add one from the Component menu.

When the user clicks on an object with a collider, a MouseDown event occurs. You can catch this event by adding an OnMouseDown() method to your script.

  1. Add an OnMouseDown() event handler to the script on the target prefab.
  2. Make the object destroy itself when it is clicked.

When all the targets are destroyed, we want to start a new level with twice as many. To keep track of the number of live targets we can add them all as children of the joint object and use Transform.childCount to see how many are left.

  1. Add an Update method to the joint object which checks how many children are left.
  2. If there are no children left, create twice as many as before.

Challenge: Rather than making objects disappear immediately when clicked, make them perform some kind of ‘dying’ behaviour (change colour, spin around, explode or something).

No Comments so far ↓

There are no comments yet...Kick things off by filling out the form below.

You must log in to post a comment.