Seagulls need to update their current waypoint — either teleporting when too far or moving to the next waypoint when close. This helps ensure smooth and natural movement ✨.

private void UpdateWaypoint()
{
    if (activeStats.isChasingPlayer || currentWaypoint == null) { return; }

    // Cache the current waypoint transform to reduce redundant calls 🏞️
    if (currentWaypointTransform == null || currentWaypointTransform != currentWaypoint.ReturnActiveWaypointTransform())
    {
        currentWaypointTransform = currentWaypoint.ReturnActiveWaypointTransform();
    }

    float distanceFromWaypoint = Vector3.Distance(transform.position, currentWaypointTransform.position);

    // If the seagull is too far, teleport to the waypoint 🏃‍♂️
    if (distanceFromWaypoint >= 175) { transform.position = currentWaypointTransform.position; }
    // If within close range, move to the next waypoint 🛣️
    else if (distanceFromWaypoint <= 5) { currentWaypoint = currentWaypoint.ReturnNextWaypoint(); }
}

What the Code Does:

  • Waypoint Check: First, the code ensures the seagull isn’t chasing the player. It then checks if a waypoint is assigned to the seagull.

  • currentWaypointTransform: This variable stores the current waypoint’s position. We cache it for efficiency, avoiding redundant calls to fetch the transform.

  • Vector3.Distance: This calculates the distance between the seagull and the current waypoint, which determines whether the seagull needs to teleport (if too far) or move to the next waypoint (if close).

  • Waypoint Update: If the seagull is too far from the waypoint, it "teleports" to it. If it's close enough, it updates to the next waypoint.

Key Concept:

This lesson focuses on efficiently moving the seagull between waypoints. We optimize by caching transforms and updating waypoints only when necessary. 📍