Game Development Reference
In-Depth Information
public float thresh;
public GameObject trackObj;
public GameObject baseObj;
4. We want to provide an implementation for the
eval()
method that was de-
clared in the NPC base. To do this, note the syntax
public override
bool
, which is as follows:
public override bool eval()
5. The
eval()
method will check the distance between the two GameObjects
trackObj
and
baseObj
for each frame. Don't forget these need to be set in
the inspector; they can be the NPCs and the player or any two GameObjects
for that matter (objects that have a transform):
bool rval = false;
Vector3 vDisp =
(this.baseObj.transform.position
-trackObj.transform.position);
float dist = vDisp.magnitude;
if ( dist < thresh)
rval = true;
return rval;
Congratulations, you have written a condition script that tests whether two GameOb-
jects are closer than a set threshold.
Implementing the condition_fartherThanThresh
script
Let's create a condition to check if the NPC is far enough from the player.
1. Create a new script called
condition_fartherThanthresh
.
2. In MonoDevelop, edit the signature of the class declaration so that it inherits
from
npcCondition
rather than
MonoBehaviour
. Also add the explicit
serialization tag to this class so the editor can save it: