Game Development Reference
In-Depth Information
if (destroyWhenFinished){
Destroy (doorOpenSound);
Destroy (gameObject);
}
}
Why?
Note that we declared a new variable (openAngle) that is defined as a
float (a numerical value that can include a decimal). Then, in the iTween
command, for the Y value, instead of listing a number, we plug in the
variable (now listed on Trigger-BulkheadDoor1 in the Inspector as Open
Angle). So when Unity runs this line, it checks for what the game designer
has defined the angle to be. This will add tremendous flexibility since not
all doors will open the same amount.
Conclusion
And with that we're going to leave triggers. They can be a very handy way to
make things happen as a character moves through the scene.
Triggers are handy, but sometimes clumsy. A trigger can cause problems if
there are other things moving in the scene. If, for instance, there is an object
animated in the scene that has a collider on it and it passes through the
trigger, suddenly things can begin happening that are only meant to happen
when the character walks through it. Some of this can be mitigated by
checking the object colliding with the trigger for specific names or tags, but
this takes some extra overhead.
Alternately, there are some exciting and interesting alternatives to triggers that
make use of Unity's raycasting functionality. In the next chapter we'll start to
look at this very powerful ability in Unity and how to make use of it in our game.
The fun is just beginning.
Homework and Challenges
Challenge 1: In this chapter we looked at using triggers to open doors, and
we've looked at using triggers to provide on-screen hints. Scene-Hallway is
full of doors that shouldn't open. Create triggers for these doors that bring
up a screen hint indicating that “This door appears to be locked or sealed.”
Challenge 2: Our current method of displaying screen hints when we hit a
trigger did some important things in illustrating communication between
scripts. However, it's actually more complicated than it needs to be. Design
a script that is applied to any triggers that are meant to display screen
hints, that simply defines what the text should be, turns the text on, waits
5 seconds, and then turns the script off. Even better, make it so flexible
that we can enter the message in the Inspector on a trigger by trigger
basis and not have to hard code it into the script (one solution is included
in the downloadable versions of the game as ScreenHintsTriggerScript).
 
Search WWH ::




Custom Search