navmeshAgent + animation trigers = issue

Hello Unity Community )

In the end of this lesson: Learn game development w/ Unity | Courses & tutorials in game design, VR, AR, & Real-time 3D | Unity Learn player is still using walking animation clip while striking enemies…

How could i fix this issue by script? I wanna to play Idle animation while player is shooting enemies.

Anyways, GREAT thanks for tutorial!

Here is the script:

using UnityEngine;
using System.Collections;

namespace CompleteProject
{

    public class ClickToMove : MonoBehaviour {

        public float shootDistance = 10f;
        public float shootRate = .5f;
        public PlayerShooting shootingScript;

        private Animator anim;
        private NavMeshAgent navMeshAgent;
        private Transform targetedEnemy;
        private Ray shootRay;
        private RaycastHit shootHit;
        private bool walking;
        private bool enemyClicked;
        private float nextFire;

        // Use this for initialization
        void Awake ()
        {
            anim = GetComponent<Animator> ();
            navMeshAgent = GetComponent<NavMeshAgent> ();
        }
       
        // Update is called once per frame
        void Update ()
        {
            Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
            RaycastHit hit;
            if (Input.GetButtonDown ("Fire2"))
            {
                if (Physics.Raycast(ray, out hit, 100))
                {
                    if (hit.collider.CompareTag("Enemy"))
                    {
                        targetedEnemy = hit.transform;
                        enemyClicked = true;
                    }

                    else
                    {
                        walking = true;
                        enemyClicked = false;
                        navMeshAgent.destination = hit.point;
                        navMeshAgent.Resume();
                    }
                }
            }

            if (enemyClicked) {
                MoveAndShoot();
            }

            if (navMeshAgent.remainingDistance <= navMeshAgent.stoppingDistance) {
                if (!navMeshAgent.hasPath || Mathf.Abs (navMeshAgent.velocity.sqrMagnitude) < float.Epsilon)
                walking = false;
            } else {
                walking = true;
            }

            anim.SetBool ("IsWalking", walking);
        }

        private void MoveAndShoot()
        {
            if (targetedEnemy == null)
                return;
            navMeshAgent.destination = targetedEnemy.position;
            if (navMeshAgent.remainingDistance >= shootDistance) {
           
                navMeshAgent.Resume();
                walking = true;
            }

            if (navMeshAgent.remainingDistance <= shootDistance) {
           
                transform.LookAt(targetedEnemy);
                Vector3 dirToShoot = targetedEnemy.transform.position - transform.position;
                if (Time.time > nextFire)
                {
                    nextFire = Time.time + shootRate;
                    shootingScript.Shoot(dirToShoot);
                }
                navMeshAgent.Stop();
                walking = false;
            }
        }

    }

}

Please format your code. Helping is easier, if we can read the code. Thank you.

DONE )

the boolean set on line 89 can be overwritten by the boolean set on line 62 if the destination hasn’t been reached.

You might want to consider changing the order of the two if groups on lines 54 and 58?

1 Like

THX!

But when i done it, player change his animation from running to Idle when he is shooting the enemy, but after enemy is killed, player start Walking animation instead of Idle animation.
If i am right, the main problem is in the navMeshAgent.destination = targetedEnemy.position (stroke 72) ; :frowning:

Player still have an end destination by navMeshAgent after killing an enemy, any solutions?

OK, this problem solved by changing value navMeshAgent.destination = player.tranform.position after enemy health <=0
Anyway, GREAT THANKS!