So my enemy stays in running animation with this script here that I picked up off a dev tutorial so how can I get the enemy to go back to Idle and stop moving after player after visDist is greater for the two objects, like can I add check to see if visDist is greater or what?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AIController : MonoBehaviour
{
Animator anim;
public Transform player;
public float rotationSpeed = 2.0f;
public float speed = 2.0f;
public float visDist = 20.0f;
public float visAngle = 30.0f;
public float shootDist = 5.0f;
string state = "IDLE";
void Start()
{
anim = this.GetComponent<Animator>();
}
void Update()
{
Vector3 direction = player.position - this.transform.position;
float angle = Vector3.Angle(direction, this.transform.forward);
if(direction.magnitude < visDist && angle < visAngle)
{
direction.y = 0;
this.transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(direction), Time.deltaTime * rotationSpeed);
if(direction.magnitude > shootDist)
{
if(state != "RUNNING")
{
state = "RUNNING";
anim.SetTrigger("isRunning");
}
}
else
{
if(state != "SHOOTING" && direction.magnitude < shootDist)
{
state = "SHOOTING";
anim.SetTrigger("isShooting");
}
}
}
else
{
if(state != "IDLE")
{
state = "IDLE";
anim.SetTrigger("isIdle");
}
}
if(state == "RUNNING")
this.transform.Translate(0,0, Time.deltaTime * speed);
}
}