Hello,
I finaly got the running animation for my enemy to work, but the problem is that it wont stop running.
the idea is that the enemy starts running to the player once the player is inside the circle where the enemy can see it. then once the enemy eighter reaches the player or the player goes out of sight the enemy needs to stop running and go back to the idle state. I have been trying and looking for answers for 4 hours now and i realy dont know how to fix this.
this is the script i have on the enemy now
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class FindAndFollow : MonoBehaviour
{
public float LookRadius = 100f;
Transform target;
NavMeshAgent agent;
Animator anim;
// Start is called before the first frame update
void Start()
{
target = PlayerManager.instance.player.transform;
agent = GetComponent<NavMeshAgent>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
float distance = Vector3.Distance(target.position, transform.position);
if (distance <= LookRadius)
{
agent.SetDestination(target.position);
agent.isStopped = false;
anim.SetBool("IsRunning", true);
}
if ( distance <= agent.stoppingDistance)
{
FaceTarget();
}
if (distance >= LookRadius)
{
agent.isStopped = true;
anim.SetBool("IsRunning", false);
}
if (LookRadius == 10f)
{
agent.isStopped = true;
anim.SetBool("IsRunning", false);
}
}
void FaceTarget()
{
Vector3 direction = (target.position - transform.position).normalized;
Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 5f);
}
void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, LookRadius);
}
}
i realy hope someone can help me fix this