Problems with FleePath accuracy

Hi!

Are there any tricks to making flee path more accurate in where it paths to? I’m trying to create an archer AI in a turn based game where they run to maximum range of their attack away from the enemy. Problems I’m having right now are that sometimes the AI moves for only about 1 unit and sometimes it moves past the maximum range.

Here’s my current code:

    public IEnumerator MoveToMaxRange () {
      float range = chosenAbility.Range - 0.5f;
      Vector3 direction = transform.position - target.transform.position;
      Vector3 destination = target.transform.position + (direction.normalized * range);

      FleePath path = FleePath.Construct (transform.position, target.transform.position, (int) range * 1000);
      path.aimStrength = 99;
      path.spread = 600;
      path.aim = destination;

      Seeker seeker = GetComponent<Seeker> ();

      characterMovement.ai.isStopped = false;
      characterMovement.ai.canSearch = false;
      characterMovement.ai.SetPath (null);

      seeker.StartPath (path);

      yield return StartCoroutine (path.WaitForPath ());

      while (!characterMovement.ai.reachedEndOfPath && !characterMovement.ai.reachedDestination && CanMove ()) {
        yield return null;
      }

      MovedThisTurn = true;
    }

Hi

The FleePath should always pick a point within the cost range [(int) range * 1000, (int) range * 1000 + 600] in your case. Are you seeing something else happening? Note that you might want to use

(int)(range * 1000) instead of (int)range * 1000 to avoid a loss of precision.

Hmm, okay a few questions:

  1. If the flee point is always WITHIN the cost range does that mean the actual distance could be anything from 0 to 5 units?
  2. Is there any way to make the minimum atleast let’s say half of the desired distance?
  3. And is the range calculated from start position and not avoid position? In that case I would need to subtract the current distance to the target from the range.

Hi

When within the cost range [(int) range * 1000, (int) range * 1000 + 600] I mean it will be

(int) range * 1000 <= cost <= (int) range * 1000 + 600]

  1. Yes. The range is calculated from the start position.
1 Like

Thank you! That helps, I think I can make it work now.

1 Like