Making your ai attack then return to the navmesh when player leaves trigger
So i want my ai to attack the player when he enters the trigger and he does but when i leave the trigger he continues to do th punching animation and also freezes ill add the scripts so you can kinda see what im trying to get im a complete noob at this but ill explain also. i have my ai following a nav of a straight line walking back and forth as a patrol guard i want my ai to attack my player when he enters the ai trigger zone and when the player leaves the trigger zone i want my ai to stop punching an resume the nav patrol he originally was doing also when i walk up on my ai from behind he just starts punching he doesnt turn around and fight the player just starts punching were he is facing oh also when the punching animation starts its fine but at the end the ai walks towards the player even though he already in the trigger then he starts to punch again is there a way i can make him continuously punch without stoping and walking then punching stopping walking then punching thank you in advance
using UnityEngine;
using System.Collections;
public class AI_Attack : MonoBehaviour {
public Transform player;
public NavMeshAgent guard;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
if (Vector3.Distance (player.position, this.transform.position) <10 )
{
Vector3 direction = player.position - this.transform.position;
this.transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(direction),0.1f);
}
}
}
using UnityEngine;
using System.Collections;
public class AI_Animations : MonoBehaviour {
Animator animator;
bool attackAnim;
void Start ()
{
attackAnim = false;
animator = GetComponent<Animator> ();
}
void OnTriggerEnter(Collider col )
{
if(col.gameObject.tag == "Player")
{
attackAnim = true;
killer ("Attack");
}
}
void OnTriggerExit(Collider col )
{
if(attackAnim)
{
attackAnim = false;
killer ("Walk");
}
}
void killer(string direction)
{
animator.SetTrigger(direction);
}
}