Game Development Reference
In-Depth Information
Objective complete - mini debriefing
We just created the
RocksTrigger
and
Rocks
scripts, which are used to trigger the
rockslide
event when the player hits the trigger area. First, we created the
Rocks
script and used
OnTriggerEnter()
to check whether the player has entered the trigger
area or not. Then, we call the
onTrigger()
event function to send the event to the
listener, which is
RocksTrigger
, using the
delegate
and
event
functions in C#.
However, there are no
delegate
and
event
functions in Unity JavaScript. So, we have
to use our custom script, which is the
JSDelegate
script available in the
Chapter7/
Scripts/Javascript
folder in the
Project
view, which already comes with the pro-
ject's package.
Next, we listen to the trigger by adding the following script:
// Unity JavaScript user:
function OnEnable () : void
{
RocksTrigger.onTrigger.Add(OnTrigger);
}
function OnDisble () : void
{
RocksTrigger.onTrigger.Remove(OnTrigger);
}
function OnTrigger () : void
{
EnabledRigidbody();
}
// C# user:
void OnEnable ()
{
RocksTrigger.onTrigger += OnTrigger;