Similar But Without Fractions - Static Speed Settings

So, I did the challenge, before he explained the challenge of not to do it on the Live Code… opps, but it worked out. I started in the same place, I went to Mover.cs, since it deals with the movement, and found where we are editing the navMeshAgent:

public void MoveTo(Vector3 destination, float moveSpeed)
        {
            navMeshAgent.destination = destination;
            navMeshAgent.isStopped = false;
            navMeshAgent.speed = moveSpeed;
        }

Then, I just followed the red lines around Visual Studio:

public void StartMoveAction(Vector3 destination, float moveSpeed)
        {
            GetComponent<ActionScheduler>().StartAction(this);
            MoveTo(destination, moveSpeed);
        }

I ran into a conundrum in the Fighter.cs, it took a bit as I had to follow the logic backwards, we’re moving… when? Only after a target is set, is not dead, and is in range… okay, so that starts all the way back at Attack(), well, I’ll just set the moveSpeed from the public Attack() method:

public void Attack(GameObject combatTarget, float moveSpeed)
        {
            GetComponent<ActionScheduler>().StartAction(this);
            target = combatTarget.GetComponent<Health>();
            moverSpeed = moveSpeed;
        }

Then pass that back to Mover (you can establish the variable up top simply as ‘float moverSpeed;’ :

private void Update()
        {
            timeSinceLastAttack += Time.deltaTime;

            if (target == null) return;
            if (target.IsDead()) return;

            if (!GetIsInRange())
            {
                GetComponent<Mover>().MoveTo(target.transform.position, moverSpeed);
            }
            else
            {
                GetComponent<Mover>().Cancel();
                AttackBehaviour();
            }
        }

Okay - so Fighter and Mover are both happy with each other. Now all that was left was to have the AI and Player Controllers pick their moveSpeed. In the future I can have a walk/run speed for player, but currently just set player at 5.66f to match animation speed. Enemies I gave a patrol speed of a slower amount, and a chase speed slightly higher, but all tune-able in inspector:
AIController:

[SerializeField] float patrolSpeed = 3f;
[SerializeField] float chaseSpeed = 4.5f;
private void AttackBehavior()
        {
            timeSinceLastSawPlayer = 0f;
            fighter.Attack(player, chaseSpeed);
        }
private void PatrolBehavior()
        {
            Vector3 nextPosition = guardPosition;
            

            if(patrolPath != null)
            {
                if (AtWaypoint())
                {
                    timeSpentDwelling += Time.deltaTime;
                    if (timeSpentDwelling > dwellTime)
                    {
                        CycleWaypoint();
                    }
                }
                nextPosition = GetCurrentWaypoint();
            }
            mover.StartMoveAction(nextPosition, patrolSpeed);
        }

And the player, similarly - add a Serialized variable, and adjust the two calls to Fighter and Mover:

 GetComponent<Fighter>().Attack(target.gameObject, playerSpeed);
GetComponent<Mover>().StartMoveAction(hit.point, playerSpeed);

Works great - and all configuration is done in a single source for player and AI - so you can tune each enemy.

Privacy & Terms