Search Unity

Procedural IA Path

Discussion in 'Scripting' started by Black_Raptor, Jun 11, 2018.

  1. Black_Raptor

    Black_Raptor

    Joined:
    Nov 3, 2014
    Posts:
    181
    Hi !

    I'm asking my self if it's who can i do for make an procedural path :

    - I have an Enemy spawner, when my Enemy spawn they look for their traget ( Their traget is static and never move )
    - I use nav mesh system for that, so when my Enemy spawn they always take the shortest way to go at their traget

    So now that's i would it's to make some variant path between all my Enemy for avoid this ( The fact that they always take the same path to the traget ).

    Someone have an idea ?
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    You want the AI to take a completely random path each time? NavMesh obviously tries to provide the shortest path, since that's what is almost always wanted. But there are a couple of things you could do to avoid the shortest path.

    First, you can create areas in your navmesh that are more expensive to traverse. For example, you could mark a certain part of your mesh as being water, and your AI would probably avoid it because it would be more expensive to go through it than to go around it. But that would need to be baked into your navmesh, so I don't think you could adjust that dynamically/randomly at runtime. More on that: https://docs.unity3d.com/Manual/nav-AreasAndCosts.html

    Second, and probably more useful in your case, you can use Navmesh Obstacles to block the path:

    https://docs.unity3d.com/Manual/nav-CreateNavMeshObstacle.html

    You'd have to be careful about how you used it, but the basic idea is that you could create invisible obstacle which prevent the AI from going to that location. If you added a few obstacles you could create sort of a maze the AI needs to move though, which would prevent it from going in a straight line to the target.

    Third, and maybe the easiest approach: Don't have your AI go straight to its target. Instead, determine some intermediate positions that are roughly ahead of the AI. For example, assume the AI starts at the red circle and the target is the blue circle:

    upload_2018-6-11_11-30-58.png
    Instead of giving the AI the destination of the blue circle, give it a destination that is somewhat between the red and blue circle. When the AI reaches that first destination, give it a new destination that's a bit closer. Keep doing that until you're close enough to provide the AI with the blue circle as the destination.
     
  3. Black_Raptor

    Black_Raptor

    Joined:
    Nov 3, 2014
    Posts:
    181
    Thanks for your answer.

    It's exactly what i did, but the problem is the path was baked so all the IA will tkae this same path, i look for news path generation for each IA spawning :/.
     
  4. bakir-omarov

    bakir-omarov

    Joined:
    Aug 1, 2015
    Posts:
    48
    If think you don't get @dgoyette's idea. He mean you can just give your enemy a random positions on your bakedNavmesh area. For example:

    Create Enemy > Set his Target first blue Circle > if he reaches first blue Circle > Set his Target second blue Circle > ... > if he reached last blue circle > set the target Red circle (your REAL target).

    So your enemies will generate a random path each time!

    2018-06-12_13-32-15.jpg
     
  5. Black_Raptor

    Black_Raptor

    Joined:
    Nov 3, 2014
    Posts:
    181
    So i need to generate procedural blue circle along the path for that no ? Otherwise it will be the same, all the enemy go to the fisrt blue circle with the shotest way to join him
     
  6. bakir-omarov

    bakir-omarov

    Joined:
    Aug 1, 2015
    Posts:
    48
    Yeah, of course. Each enemy will generate blue circles for himself. And it will be random as you want.
     
  7. Black_Raptor

    Black_Raptor

    Joined:
    Nov 3, 2014
    Posts:
    181
    Oooooooooooooh ok i got it !! Thanks !