Translate

jueves, 1 de enero de 2015

Unity3D - Easily Handling Player's Objectives

A really easy way to handle player's objectives in Unity3D, is by taking advantage of these features:

Game Object hierarchy

Using public fields in scripts

Drag and Drop objects into public fields

With that said, this is what we can do:

Create an empty game object

Create a new child object per objective we want to represent

Move each objective to the corresponding position(To make it easier you can click on the cube in the inspector once you have selected the objective, this will show the icon and name of the object in the Scene view)

Attach a collider marked with trigger so that it does not block the player movement.

Create a custom Objective script which will contain it's Name, Description, and other aspects required to handle it's behavior, including the action to perform when collider is reached. One of the important keys here is that each objective would have a "NextObjective" field, which is of the same type of the script(Objective). This helps reducing harcoded logic strings to go into the next objective.

Create a Objectives script, which will contain a field for CurrentObjective and internally retrieves the list of all of the objectives in the hierarchy.

Attach the Objectives script into the player's object, and drag your initial objective into the CurrentObjective field