Hi guys im trying to implement animator in my click to move game but right now im haveing an issue, the idle works fine but when i try run the character keep idle and only sometimes turn to running.
I hv my animator like the image bellow with 2 parameters, so i change idle to run when IsRunning is true and vice-versa:
- bool IsRunning
- trigger Die
And i hv this script attached to player, can anyone help plz???
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ClickToMove : MonoBehaviour
{
NavMeshAgent navAgent;
public float attackDistance = 10f;
public float attackRate = 0.5f;
private Animator anim;
private Transform targetedEnemy;
private Ray shootRay;
private RaycastHit shootHit;
private bool running;
private bool enemyClicked;
private float nextHit;
// Use this for initialization
void Awake ()
{
anim = GetComponent<Animator>();
navAgent = GetComponent<NavMeshAgent>();
}
void Update()
{
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Input.GetMouseButtonDown(0))
{
if(Physics.Raycast(ray, out hit, 100))
{
if(hit.collider.CompareTag("Enemy"))
{
targetedEnemy = hit.transform;
enemyClicked = true;
}
else
{
running = true;
enemyClicked = false;
navAgent.destination = hit.point;
navAgent.Resume();
}
}
}
if (enemyClicked)
{
MoveAndHit();
}
if (navAgent.remainingDistance <= navAgent.stoppingDistance)
{
running = false;
}
else
{
running = true;
}
anim.SetBool ("IsRunning", running);
}
private void MoveAndHit()
{
if (targetedEnemy == null)
return;
navAgent.destination = targetedEnemy.position;
if (navAgent.remainingDistance >= attackDistance)
{
navAgent.Resume();
running = true;
}
if (navAgent.remainingDistance <= attackDistance)
{
transform.LookAt(targetedEnemy);
Vector3 dirToHit = targetedEnemy.transform.position - transform.position;
if(Time.time > nextHit)
{
nextHit = Time.time + attackRate;
//attackmethod
}
//navAgent.Stop();
//running = false;
}
}
}