Game Development Reference
In-Depth Information
transform.Rotate(rotAxis, rotSpeed *
Time.deltaTime);
7. The
OnTriggerEnter()
method will be invoked whenever the collider
of this object collides with another collider in the world; incidentally, if we
set
IsTrigger=false
on our
gameObject
, the
OnCollisionEnter()
method will be dispatched instead of
OnTriggerEnter()
. Note, for Unity
to dispatch either of these callbacks, we must remember to add a
Rigidbody
component to the GameObject of
InteractiveObj
at the design time in
the editor.
8. Note, when Unity dispatches this callback, it passes in another parameter of
the
collider
type. This collider is the collider of the object that entered the
trigger volume. Convenient! The signature looks as follows:
OnTriggerEnter(other collider)
{
}
9. In this method, we check that the other object (the
gameObject
that has just
entered this collider) has a tag equal to
Player
, as shown in the next line
of code. This is how we ensure that our trigger only responds to entities that
we specify (we must remember to set the tag on the player
gameObject
to
Player
):
if (other.gameObject.tag == "Player")
10. If the
OnCloseEnough
object interaction is not null, we dereference it and
invoke the
handleInteraction()
method. In our example, this method
does the work of inserting objects into the inventory as shown in the following
code:
if (OnCloseEnough != null)
{
OnCloseEnough.handleInteraction();
}
Congratulations! We now have a class that implements an interactive object. Let's
continue further with an
ObjectInteraction
script that this class can utilize.