Game Development Reference
In-Depth Information
var position = target.position;
var direction = position — transform.position;
direction.y = 0;
// Rotate towards the target
transform.rotation = Quaternion.Slerp (
transform.rotation, Quaternion.
LookRotation(direction),
rotationSpeed * Time.deltaTime);
transform.eulerAngles = Vector3(0,transform.
eulerAngles.y, 0);
// Move the character
if(direction.magnitude > keepDistance)
{
direction = direction.normalized * speed;
transform.position += direction;
}
}
function Shoot()
{
var position = target.position;
var direction = position - transform.position;
direction.y = 0;
// Rotate towards the target
transform.rotation = Quaternion.Slerp(transform.rotation,
Quaternion.LookRotation(direction),
rotationSpeed * Time.deltaTime);
transform.eulerAngles = Vector3(0, transform.
eulerAngles.y, 0);
}
Step 3. Locate the AI script in the Inspector attached to the
robot object in the Hierarchy. Drag and drop the First
Person Controller from the Hierarchy onto the exposed
target variable. This sets the object that the NPC will consider its
enemy.
Step 4. Play. The CanSeeTarget() and CanShoot() functions use
the variables seeRange and shootRange , respectively, to
determine when the player is close enough to pursue and
close enough to shoot. If you run away from the NPC backward
as fast as possible you'll notice the point at which it gives up
chasing you.
Search Nedrilad ::




Custom Search