You are on page 1of 2

using UnityEngine;

[RequireComponent(typeof(NavMeshAgent))]
[RequireComponent(typeof(ThirdPersonCharacter))]
public class AICharacterControl : MonoBehaviour {
public NavMeshAgent agent { get; private set; } // the navme
sh agent required for the path finding
public ThirdPersonCharacter character { get; private set; } // the chara
cter we are controlling
public Transform target;
// target to aim for
public float targetChangeTolerance = 1;
// distance to target before target can be changed
Vector3 targetPos;
// Use this for initialization
void Start () {
// get the components on the object we need ( should not be null due to
require component so no need to check )
agent = GetComponentInChildren<NavMeshAgent>();
character = GetComponent<ThirdPersonCharacter>();
}
// Update is called once per frame
void Update () {
if (target != null)
{
// update the progress if the character has made it to the previ
ous target
if ((target.position-targetPos).magnitude > targetChange
Tolerance) {
targetPos = target.position;
agent.SetDestination(targetPos);
}
// update the agents posiiton
agent.transform.position = transform.position;
// use the values to move the character
character.Move( agent.desiredVelocity, false, false, tar
getPos );
} else {
// We still need to call the character's move function,
but we send zeroed input as the move param.
character.Move ( Vector3.zero, false, false, transform.p
osition + transform.forward * 100 );
}
}
public void SetTarget(Transform target)
{
this.target = target;
}
}