Game Development Reference
In-Depth Information
Why?
Of course we haven't made the function that actually does the damage
yet (DoDamage), but we'll create that in a minute. Do notice, however,
that in the last command in that block of code we're telling Unity to go
fire DoDamage, but take with it the gameObject attached to other (which
is the collider that entered the trigger).
Step 18: Add a function to set inTrigger to false when the player exits the
trigger:
var damageAmount : int;
private var inTrigger : boolean;
function OnTriggerEnter (other:Collider){
if (other.gameObject.tag == “Player”){
inTrigger = true;
DoDamage (other.gameObject);
}
}
function OnTriggerExit (other:Collider){
inTrigger = false;
}
Why?
For some of the triggers this will be used on, the player will never leave
it alive. But, the player could enter the water trigger, and then climb out
via the terrain. So we need Unity to keep track of whether the player has
emerged alive or not.
Broadcast Message
We've looked at a few ways to communicate between objects and scripts.
There's one final method we'll look at in this topic—a powerful method
called Broadcast Message. What Broadcast Message does is shout a message
out and objects that are listening for it hear it and obey. In this game we're
using Broadcast Message in a fairly targeted fashion, but consider a situation
in which lots of objects in a scene need to do something (say enemies
needing to reset their target); using Broadcast Message, a weapon could
“shout” to all of them at once, “oy! Now go chase this little distraction robot
I've launched!” Using the past methods of finding an object, then accessing
a specific script on that object, and then accessing a specific function, would
be problematic if there were 100 enemies on screen. But with Broadcast
Message, the enemies would hear the command, and all fire their respective
function to realign target.
Warnings and Pitfalls
There are some very
specific designed
restrictions to Broadcast
Message that have to
do with where objects
are within Hierarchy and
who hears a message.
So if you plan to use this
extensively in a project
be sure to read the
documentation on these
sorts of issues.
Search WWH ::




Custom Search