Game Development Reference
In-Depth Information
public class condition_onEnter :
npcCondition;
10. Give the
condition_OnEnter
class a public reference to a GameObject
called
trackObj
. This will hold a reference to the most recent GameObject
that enters the trigger volume. This can be done with the following line of
code:
public GameObject trackObj;
11. Add a private Boolean variable named
hasEntered
, and initialize it to
false
. This will be used to track whether an object is actually inside the
volume (rather than only having entered this frame):
private bool hasEntered = false;
12. Now recall that the
OnTriggerEnter
/
OnTriggerExit
callbacks are re-
turned from the physics system rather than our DecisionMgr system. In order
to interface the two, we will implement
OnTriggerEnter
and
OnTrigger-
Exit
and then pass the relevant information outward.
13. The
OnTriggerEnter
method should simply set
hasEntered
to
true
,
and it should set
trackObj
to
other.gameObject
as shown in the follow-
ing code snippet:
void OnTriggerEnter(collider other)
{
hasEntered = true;
trackObj = other.gameObject;
}
14. The
OnTriggerEnter
method should conversely set
hasEntered
to
false
, and it should nullify
trackObj
as shown in the following code snip-
pet:
void OnTriggerExit(Collider other)
{
hasEntered = false;