Search Unity

Fleeeing AI Help: Need a new evaluation approach

Discussion in 'Scripting' started by Nanako, Feb 6, 2016.

  1. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    Hi all, i'm writing some AI for a fleeing prey animal, that tries to instinctively run from the player.

    I've got a somewhat complex system of path selection up and running, things mostly work well, but i'm having one slight issue.

    The animal frequently evaluates its current path, by checking the distance from the predator (ie, the player). If it's farther away than last frame, it assumes the path is good, and doesn't change direction. If the predator has gotten closer since last frame, it increments a timer, and changes direction if that timer rises too high.

    This system copes well with chases across open terrain, but it's not very good at handling short bursts of catching, for example due to exploiting terrain, or leaping. If the predator is normally slower, but catches up briefly due to some burst in speed, the prey can't register that, it sees the predator getting closer for a short amount of time, (which is often less than the timeout) and then sees it getting farther away again as it keeps running, causing it to never switch paths. This repeats endlessly while the predator repeatedly jumps and bites it, not good.

    I think just comparing with the last frame is the issue here, i need a better approach to evaluating the success or failure of the current path. Shortening the direction change timeout doesn't seem like a good solution either.

    I have a vague idea about measuring average distance over some time period, but i'm not certain how, or if that might work. i could do with some input.
     
  2. Teravisor

    Teravisor

    Joined:
    Dec 29, 2014
    Posts:
    654
    You have direction predator is from prey. You have path's current direction.
    Now how about checking if dot product of those if positive or negative before acting?(Getting closer of farther)

    Also if there's no need for pathfinding itself, just obstacle avoidance and fleeing I recommend steering behaviours. Like in forest or open area where you can just avoid tree. Won't work good in rooms/buildings and cluttered areas though.
     
    Last edited: Feb 6, 2016
  3. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    Perhaps there's a miscommunication. I have no problem choosing directions. The issue is just making the animal know when to correct its heading, and when to keep going
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    Are you looking for a way to make the prey feint? Something along the lines of "the predator will catch me soon, so I should change direction immediately?"

    I guess you could do some prediction to fix your issue - if the predator's current velocity means that it will catch up with the prey in less time than the timer, dodge in some direction. So something like:

    Code (csharp):
    1. bool ShouldDodge(pred_position, prey_position, pred_velocity, prey_velocity) {
    2.     distance = (prey_position - pred_position).magnitude;
    3.    
    4.     expected_time_before_catch = distance / (pred_velocity - prey_velocity);
    5.  
    6.     if(expected_time_before_catch < timer_max_limit) {
    7.         return true;
    8.     }
    9.     return false;
    10. }
    11.  
    12.  
    This will mean that if the predator does a leap forward, it would trigger the prey to dodge to the right or left. If you couple that with the prey having a lower speed, but a better turning rate than the predator, you'll get something that looks like a lion chasing antilopes - desperate left/right duking.



    This is a really hard problem, because you're not looking for a way to make the prey able to dodge every attack - just a way to dodge many of them. Making a system where AI-driven agents interact with each other in ways that's interesting to players is probably the most difficult thing you can do. If the predator in this case is the player, it might be easier, because you'll want to build a system that's exploitable by skill, as that's fun.