Solution to enemy walk and run speed

I first made a function AttackPlayerInRange() within AIController.cs to return a bool and made it public.

AIController.cs

        // Update is called once per frame
        void Update()
        {
            if (health.IsDead()) return;
            AttackIfPlayerInRange();
        }

                public bool AttackIfPlayerInRange()
        {           
            distanceToPlayer = Vector3.Distance(transform.position, player.transform.position);
            if (distanceToPlayer <= chaseDistance && fighter.CanAttack(player))
            {               
                AttackBehaviour();
                return true;
            }
            else if (timeSinceLastSawPlayer < timeToWaitSuspiciously)
            {
                SuspicionBehaviour();
            }
            else
            {
                PatrolGuardBehaviour();
            }
            if (navMeshAgent.velocity.magnitude < 0.1f)
                UpdateTimers();
            return false;
        }

Then within the Mover.cs I got the AIController.cs component and changed the enemy speed when AttackPlayerInRange() returned true. I also implemented a way to manage the player speed when you press R it toggles the walk and run speed or when you press hold Left Control it runs, this is the same way that Diablo 2 works.

Mover.cs

        // Start is called before the first frame update
        void Start()
        {
            navMeshAgent = GetComponent<NavMeshAgent>();
            anim = GetComponent<Animator>();
            health = GetComponent<Health>();

            if (gameObject.CompareTag(ENEMY_TAG))
                aiController = GetComponent<AIController>();
        }

        // Update is called once per frame
        void Update()
        {
            navMeshAgent.enabled = !health.IsDead();
            if (health.IsDead()) return;

            if (gameObject.CompareTag(PLAYER_TAG))
                ChangePlayerWalkRunSpeed();

            if (gameObject.CompareTag(ENEMY_TAG))
                ChangeEnemyWalkRunSpeed();

            UpdateAnimator();
        }

        private void ChangeEnemyWalkRunSpeed()
        {
            if (aiController.AttackIfPlayerInRange())
                navMeshAgent.speed = enemyRunSpeed;
            else
                navMeshAgent.speed = enemyWalkSpeed;
        }

        private void ChangePlayerWalkRunSpeed()
        {
            // If Control is pressed
            if (Input.GetKeyDown(KeyCode.LeftControl))
                walkOrRun = true;
            else if (Input.GetKeyUp(KeyCode.LeftControl))
                walkOrRun = false;

            // If R is toggled
            if (!Input.GetKey(KeyCode.LeftControl))
            {
                if (!walkOrRun)
                {
                    if (Input.GetKeyDown(KeyCode.R))
                        rPressed = true;
                }
                else
                {
                    if (Input.GetKeyDown(KeyCode.R))
                        rPressed = false;
                }

                if (rPressed)
                    walkOrRun = true;
                else
                    walkOrRun = false;
            }

            // Change speed
            if (walkOrRun)
                navMeshAgent.speed = runSpeed;
            else
                navMeshAgent.speed = walkSpeed;
        }

Privacy & Terms