Game Development Reference
In-Depth Information
Objective complete - mini debriefing
In this section, we just created our AI script, which is derived from
CharacterClass
.
Most of the
CharacterClass
script is based on the
CharacterControl
script from
that we have similar methods and parameters such as the
UpdateAnimator()
function,
which get called in
OnAnimatorMove()
to control the animation state of our characters
(
Player
and
Enemy
).
We also added the new code section to give our enemy some characteristics and make it
smart enough to shoot the player, run towards the player, jump when it hits the wall, and to
stop and walk after a certain time.
In the
Run()
function, we used the following code:
var distanceToPlayer : float = (targetLookat.position -
transform.position).sqrMagnitude;
var runDistance : float =
(playerRange+shotRange)*(playerRange+shotRange);
var shotDistance : float = shotRange*shotRange;
if ((distanceToPlayer <= runDistance) && (distanceToPlayer >
shotDistance)) {
return true;
}
We used the following code to check for the distance between the enemy and the player:
if ((distanceToPlayer <= runDistance) && (distanceToPlayer >
shotDistance)) { return true; }
Have a look at the following diagram: