Seagulls move from waypoint to waypoint in the world, and their movement is controlled by the Rigidbody2D component. Here's how we handle the movement and keep the birds on a smooth path 🕊️.

private Rigidbody2D rb;
private Transform currentWaypointTransform;

public void OnEnable()
{
    if (rb == null) { rb = GetComponent(); }

    // Initialize fleeing movement speeds and rotation speeds 🏃‍♂️💨
    fleeingRotatingSpeed = 1; fleeingMovementSpeed = 1;
    randomChaseTime = Random.Range(20f, 50f);
}

private void MoveTowardsWaypoint()
{
    if (rb == null) { return; }

    // Get the dot product of velocity and transform up vector
    float velocityCheck = Vector2.Dot(transform.up, rb.velocity);
    // If velocity exceeds max velocity, stop acceleration ✋
    if (velocityCheck > activeStats.maxVelocity) { return; }

    // Move the seagull upwards by multiplying speed with direction 🦢🚀
    rb.velocity = transform.up * (activeStats.movementSpeed * fleeingMovementSpeed);
}

What the Code Does:

  • Rigidbody2D rb: This stores a reference to the seagull's Rigidbody2D, which controls its physics behavior.

  • OnEnable() Method:

    • This is a Unity lifecycle method that gets called when the object is enabled. It ensures that the seagull's Rigidbody2D is set up and ready for movement. It also sets the initial movement and rotation speeds for the seagull.

    • randomChaseTime: This is a randomized timer to control the seagull's behavior later on (how long it chases or moves randomly).

  • MoveTowardsWaypoint() Method:

    • This is where the seagull’s movement logic is applied.

    • Vector2.Dot(transform.up, rb.velocity):

      • The dot product is a mathematical operation that tells us how much two vectors align. In this case, we're checking how closely the seagull's current direction (transform.up, the up vector of the object) aligns with its current velocity (rb.velocity).

      • The dot product will return a value between -1 and 1, with:

        • 1 meaning the velocity is perfectly aligned with the direction.

        • -1 meaning it's moving in the opposite direction.

        • 0 meaning it’s perpendicular (i.e., no movement in the direction we’re interested).

      • The dot product helps us check if the seagull is moving too fast, which helps control its velocity and prevent it from exceeding its max speed.

    • rb.velocity = transform.up * (activeStats.movementSpeed * fleeingMovementSpeed);: This line applies the movement in the up direction based on the current speed, effectively moving the seagull forward.

Key Concept:

The dot product helps ensure the seagull moves smoothly and doesn't exceed its velocity limit. It checks if the seagull is moving in the correct direction relative to its current speed and adjusts accordingly. 🚀