a i character control

Download A i Character Control

If you can't read please download the document

Upload: alvaro-cortes-tellez

Post on 23-Nov-2015

12 views

Category:

Documents


0 download

TRANSCRIPT

using UnityEngine;[RequireComponent(typeof(NavMeshAgent))][RequireComponent(typeof(ThirdPersonCharacter))]public class AICharacterControl : MonoBehaviour { public NavMeshAgent agent { get; private set; } // the navmesh agent required for the path finding public ThirdPersonCharacter character { get; private set; } // the character we are controllingpublic Transform target;// target to aim forpublic float targetChangeTolerance = 1; // distance to target before target can be changedVector3 targetPos;// Use this for initializationvoid Start () { // get the components on the object we need ( should not be null due to require component so no need to check )agent = GetComponentInChildren();character = GetComponent();}// Update is called once per framevoid Update () {if (target != null){ // update the progress if the character has made it to the previous targetif ((target.position-targetPos).magnitude > targetChangeTolerance) {targetPos = target.position;agent.SetDestination(targetPos);} // update the agents posiiton agent.transform.position = transform.position; // use the values to move the charactercharacter.Move( agent.desiredVelocity, false, false, targetPos );} 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.position + transform.forward * 100 );}}public void SetTarget(Transform target){this.target = target;}}