Search Unity

Question Behavior for long range enemy when player is too close

Discussion in 'Navigation' started by juicedup, Oct 29, 2022.

  1. juicedup

    juicedup

    Joined:
    Aug 18, 2019
    Posts:
    47
    1. I'm working on the AI system for an action game. My question is about pathfinding. I have a long range enemy that pathfinds a position a bit far from the player say I want it to be 5 units away for it to stop pathfinding and do an attack. What should I do if the player gets too close? how can I tell the enemy where to go if it's TOO CLOSE
    2. keep in mind the levels are randomly generated so I can't make predetermined spots for it to go should I just make it raycast in a direction away from the player and go to the hit point if there's nothing in the way?what if the enemy is a corner?
    3. should I make it so the enemy has a short range attack and have the enemy attack until it dies?
    4. how do games typically approach this problem?
     
  2. juicedup

    juicedup

    Joined:
    Aug 18, 2019
    Posts:
    47
    btw I'm not sure if this is the right section to ask this.
    It's an AI question but navigation is the one that seemed most relevant, since my question is about where my enemy should navigate to when the player gets too close.
     
  3. androvisuals_unity

    androvisuals_unity

    Joined:
    Mar 23, 2020
    Posts:
    47
    I've done this in the following 2 ways without ever using a raycast.

    - Get the opposite direction of the unit you want to move away from. Normalize the direction so it has a length of 1.
    Create an integer with say 5 attempts and use a for loop to see if a path can be plotted 1 unit away, 2 units away etc. You'll need to just use NavmeshPath.status to check if a path is possible or not. If you hit the max amount of attempts then rotate the directional vector 90 degrees to the left or to the right and try again. If there's no valid paths behind you or to the left or right then your stuck ;)

    NavmeshPathStatus

    Another method I use much more often than the above one is to put a Sphere/circle behind the unit ( not over it ) in the direction away from the enemy. I then just pick an amount of attempts to find a random spot in the sphere again checking for a valid path. This leads to more unpredictable results but is quite easy to tweak by changing the distance from the unit and the size of the sphere.

    Random point inside circle
     
  4. juicedup

    juicedup

    Joined:
    Aug 18, 2019
    Posts:
    47
    Thank you very much for the detailed answer, the circle method seems like the easiest one to implement, I didn't even think of that.
     
    androvisuals_unity likes this.
  5. juicedup

    juicedup

    Joined:
    Aug 18, 2019
    Posts:
    47
  6. Vincent454

    Vincent454

    Joined:
    Oct 26, 2014
    Posts:
    167
    You can also do agent.raycast in random directions away from the player, to make sure they can actually go there. It will only be a straight line, but if you use a random point on the navmesh, that point might not be reachable (navmesh gets generated inside walls for example).
    Though you can also check if the random point is reachable using NavMesh.CalculatePath and checking if the path status is complete, but that might be a little bit slower, especially since you then have to find a new point until one is reachable which means it potentially could look for one forever (or you tell it to check once and if that fails, just to do the navmesh raycast then)
     
  7. androvisuals_unity

    androvisuals_unity

    Joined:
    Mar 23, 2020
    Posts:
    47
    If you set up your scene correctly and use colliders on layers to generate the navmesh I don't believe the navmesh gets generated inside closed meshes such as walls.
    Using the NavMeshPathStatus requires a path to be used and a path to be calculated. Speed on something like that is quite trivial unless there's a huge amount of objects and a massive distance between the 2 points the path is being calculated on.
     
  8. Vincent454

    Vincent454

    Joined:
    Oct 26, 2014
    Posts:
    167
    The navmesh will 100% get calculated inside objects and there is no way to prevent that sadly, unless you place navmesh obstacles with carving enabled inside the walls manually (which isnt really an option usually).
    But I guess youre right, calculating one path probably doesnt take too long, but you should still always stop after not finding a complete path a couple of times, as it might loop forever otherwise
     
  9. Inxentas

    Inxentas

    Joined:
    Jan 15, 2020
    Posts:
    278
    I prefer to have limited navcheck operations and have nothing against RayCasts, so I would opt to implement a method with a fixed amount of navmesh checks. Calculate the opposite direction of the unit you want to move away from, and have a fixed number of RayCasts going down (perhaps in a circle pattern, perhaps an XZ grid, perhaps from a certain Y offset) to find a position. Perhaps do a LOS check from those positions as well. And then prioritize them by ordering positions by distance or whatever, and pick the first one.

    That's possible. You could also have it go for melee when it cannot calculate a path to get a better shooting position.

    With an AI model. Are you using behavior trees or state machines yet? A unified way to have your enemies determine behavior is recommended. I use state machines, and my states all have scope on their state machine, which in turn has scope on all the NavMeshAgent and various Sensor components I wrote. So basically I go from state to state, with each state having Enter, Update and Exit methods to ensure that states are changed uniformly. I have these states read data from the Sensors and determine the next state accordingly.
     
  10. AnaWilliam850

    AnaWilliam850

    Joined:
    Dec 23, 2022
    Posts:
    36
    Thank you for your question, it helped me with some doubts that I had
     
    Vincent454 likes this.
  11. androvisuals_unity

    androvisuals_unity

    Joined:
    Mar 23, 2020
    Posts:
    47
    Dangit. You were right on that one, the navmesh does get built inside of objects.

    upload_2023-1-19_13-5-43.png

    upload_2023-1-19_13-5-21.png
     
    Inxentas likes this.
  12. Vincent454

    Vincent454

    Joined:
    Oct 26, 2014
    Posts:
    167
    Sadly haha. I hope one day they give us an option to prevent this from happening
     
    Inxentas likes this.
  13. Inxentas

    Inxentas

    Joined:
    Jan 15, 2020
    Posts:
    278
    This is exactly why I use raycasts with a little downward offset to really be sure when needed.