Speed with 2 Mover methods

Instead of messing with the method already in place, and having to fix everything, I created two new methods in Mover.cs

public void PatrolSpeed()
{
    agent.speed = patrolSpeed;
}

public void ChaseSpeed()
{
    agent.speed = chaseSpeed;
}

and 3 supporting fields with preset values for now (there is only one enemy type so far)

readonly float patrolSpeed = 2.66f;
readonly float chaseSpeed = 4.66f;
readonly float playerMaxSpeed = 5.66f;

On awake, set every agent to the max so the guards can run to their posts if they were distracted by butterflies before the scene started.

Then I can just call those from within AIController.cs and it doesn’t need to have any information about the movement capabilities of what it is attached to. Setting them to the proper speed when needed.

private void AttackBehavior()
{
    timeSinceLastSawPlayer = 0;
    mover.ChaseSpeed();
    fighter.MoveToAttack(playerHealth);
}
private void PatrolBehavior()
{
    Vector3 nextPosition = guardPosition;
    if(patrolPath != null)
    {
        if (AtWaypoint())
        {
            timeSinceArrivedAtWaypoint = 0;
            mover.PatrolSpeed();
            CycleWaypoint();
        }
        nextPosition = GetCurrentWaypoint();
    }
    else
    {
        if (transform.position == guardPosition)
            transform.rotation = 
                Quaternion.Lerp(transform.rotation, guardRotation, 0.05f);
    }
    if(timeSinceArrivedAtWaypoint > waypointDwellTime)
    {
        mover.StartMoveAction(nextPosition);
    }
}

And they will jog back to their path before slowing down again.

2 Likes

A good solution, it ensures some abstraction between the controller and the Mover.

i love it!

Privacy & Terms