- Home /
run animation
I have a script that when u enter in the a radios the enemy chases you when chasing after you as it movies playing the running animation
but the animation plays once and never again

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using UnityEditor.Animations;
public class Enemy : MonoBehaviour
{
public float lookRadius = 10f;
Transform target;
NavMeshAgent agent;
Animator m_Animator;
bool m_run;
// Use this for initialization
void Start()
{
target = Playermanager.instance.player.transform;
agent = GetComponent<NavMeshAgent>();
m_Animator = GetComponentInChildren<Animator>();
m_run = false;
}
// Update is called once per frame
void Update()
{
float distance = Vector3.Distance(target.position, transform.position);
if (distance <= lookRadius)
{
agent.SetDestination(target.position);
m_run = true;
m_Animator.SetBool("run", true);
if (distance <= agent.stoppingDistance)
{
// Attack the target
FaceTarget();
}
}
}
void FaceTarget()
{
Vector3 direction = (target.position - transform.position).normalized;
Quaternion lookRatation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
transform.rotation = Quaternion.Slerp(transform.rotation, lookRatation, Time.deltaTime * 5f);
}
void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, lookRadius);
}
}
animation-2.png
(42.5 kB)
Comment
Your answer