Example #1
0
    // Return the steering vector for the agent to wander, seeks a point on
    // a circle projected forward from the agent, the point moves arround the circle
    // randomly.
    public Vector2 wander()
    {
        // Update the wnader angle and Calculate the point to seek
        // The point to seek is on a circle in front of the agent at the wander angle
        // the wander angle moves randomly every frame.
        wanderAngle += Random.Range(-20, 20);
        Vector2 inFrontOfSheep   = transform.position + transform.right * 2;
        Vector2 fromCenterToEdge = new Vector2(Mathf.Cos(wanderAngle), Mathf.Sin(wanderAngle));
        Vector2 pointOnCircle    = inFrontOfSheep + fromCenterToEdge;

        return(seeker.seek(pointOnCircle));
    }
Example #2
0
    // Determine the steering vector for this agnet to pursue a target
    // with the given rigid-body
    public Vector2 pursue(Rigidbody2D target)
    {
        // Determine the time to reach the target's current location,
        // need to handle edge case to avoid dividing by 0
        Vector2 targetDirection = rigidBody.position - target.position;
        float   timeToTarget;

        if (rigidBody.velocity.magnitude < 0.001)
        {
            timeToTarget = 2;
        }
        else
        {
            timeToTarget = targetDirection.magnitude / rigidBody.velocity.magnitude;
        }
        // Project the target forward by that ammount of time
        Vector2 seekLocation = target.position + target.velocity * timeToTarget;

        // Move at max accel toward the seek location
        return(seeker.seek(seekLocation));
    }
Example #3
0
 public Vector2 seek(Vector2 point)
 {
     return(seeker.seek(point));
 }