Search Unity

Navmesh Agent - Multiple agents taking the same path?!

Discussion in 'Scripting' started by besieger1, Dec 19, 2014.

  1. besieger1

    besieger1

    Joined:
    Jul 26, 2012
    Posts:
    34
    Hi

    I have a problem with the navmesh agents all following the same path, I know this is intended but how can I make some of the agents take a different path to their target?

    I have 3 agents, Cubes all in the same position, they all take the same path. I want them to take different paths.

    I hope I was clear enough for you guys, Thanks

    PS, I tried posting this in navigation section and had had 0 replies...
     
  2. MathiasDG

    MathiasDG

    Joined:
    Jul 1, 2014
    Posts:
    114
    Pathfinding usually finds the shortest path, so if the start and goal positions are the same, the shortest path will be the same.
    Maybe you could do it using waypoints and assigning the path manually instead of calculating it with pathfinding?
    I dont really see another way of doing this with unity pathfinding.
     
    Magiichan likes this.
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    The typical way to do this is to assign a penalty to the nav mesh if a unit has already selected that path. Not sure if Unity supports this straight out of the box.
     
  4. besieger1

    besieger1

    Joined:
    Jul 26, 2012
    Posts:
    34
    how could I do this? do you have any examples showing this.
     
  5. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    maybe make different paths for each navmeshagent ?
     
  6. besieger1

    besieger1

    Joined:
    Jul 26, 2012
    Posts:
    34
    Thats what Im trying to do but how?

    To be honest I was probably expecting to much from unity's built-in AI system... I was hoping to be able to have AI taking different paths to which I then coded my own functions for handling things like Animation, Stats etc.
     
  7. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    I'm guessing you have a good understanding of coding if your using the navmeshagent, so say you have 3 players, each player goes towards 3 cubes, (cube1,cube2, cube3) you could make it so that (player1 goes towards cube2), player 2 goes towards cube 1 and player3 goes towards cube 2, but then make it so when the player1 collides with the cube using a IsTrigger I'm guessing his cube changes from cube2 to cube 3 and so on for each one, I'm guessing you would use findGameObjectwithTag for that raycasts might be good to use too.
     
  8. besieger1

    besieger1

    Joined:
    Jul 26, 2012
    Posts:
    34
    You misunderstand my OP, I have 1 Player and 3 Cubes acting as the "Enemy" on a flat plane I want each of those cubes or "Enemys" to take a slightly different path to the player.

    The idea is that if I was to have more than 10 "Enemys" to follow the player, some of them would take a different path making game play a little more difficult than being able to focus on one direction.
     
  9. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    Make the enemy's talk to each other and tell them to slow down or to go towards another object wait for a few seconds and then continue journey?
     
  10. besieger1

    besieger1

    Joined:
    Jul 26, 2012
    Posts:
    34
    That wouldn't make them take a different path, just move aimlessly then continue on the same path, I need them to make their way to their target, Just by different paths.
     
  11. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Utilize a single carve object which is reused and moved around. This will force different paths.
     
  12. besieger1

    besieger1

    Joined:
    Jul 26, 2012
    Posts:
    34
    How would I do this? an examples?

    Thanks
     
  13. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    wouldn't something like this work?
    Code (CSharp):
    1. void Start() {
    2.         agent_ = GetComponent<NavMeshAgent>();
    3.         lastPos = target.position;
    4.     }
    5.     //
    6.     void Update () {
    7.         if (Vector3.Distance(target.position,lastPos) > repathDistance) {
    8.             lastPos = target.position;
    9.             agent_.destination = target.position;
    10.         };
    11.     }
     
  14. hopeful

    hopeful

    Joined:
    Nov 20, 2013
    Posts:
    5,686
    If you are looking for a more game logic oriented solution as opposed to a technical one, maybe you could provide the individual enemies with an incentive to take different paths.

    Naturally, if the strongest incentive is to immediately engage the player, all units will take the shortest path. If there are other incentives, like to follow cover, or to avoid problem terrain, or to outflank, then there is a rationale for the enemies to take different paths according to their individual preferences.

    You could even have something like a script on the player that lobs invisible false targets a distance away from the player, within line of sight and one for each enemy that is attempting to target the player. The enemies could take one of these as their target till either they enter a trigger placed around the false target, a timer elapses, or if the player becomes closer than the false target. When any of these conditions are met, then the player becomes the true target.
     
  15. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158

    Kinda like what I said :) I know the script I posted works as it just finds a new path using the navmeshagent think it might need tweaking but 100% works :)
     
  16. besieger1

    besieger1

    Joined:
    Jul 26, 2012
    Posts:
    34
    That script will always result in the same value?

    You seem to be asking if the distance between the target and my first pos is more than the "repathDistance" value then target the player witch is already being done?

    Your asking me to target my original target if I meet a condition that was already met, do you have more script that maybe wasn't posted? as this code is only a way to compare if my target is a specific distance, unless of course I am missing something here.
     
  17. Crayz

    Crayz

    Joined:
    Mar 17, 2014
    Posts:
    194
    AFAIK Unity doesn't provide an AI system. AI and Pathfinding are also two different things. You're going to have to develop your own AI and implement pathfinding into it, rather then just tossing a navmeshagent on and expecting Unity to handle the perfect AI for you.

    Unity Pro gives you much more control over NavMeshAgents & pathfinding data, or you could try an A* mechanism:
    http://arongranberg.com/astar/

    Alternatively you could develop some sort of Waypoint generation system
    http://i.imgur.com/Kp1rWB0.jpg?1
     
    gouthammannuru likes this.
  18. besieger1

    besieger1

    Joined:
    Jul 26, 2012
    Posts:
    34
    Yea like I said I think I was expecting to much, Do you have any example to get me going on a waypoint system.

    It looks as though like you said I am going to need to develop my own AI systems to get what I am looking for even though the Unity Navmesh Agents are so close.
     
  19. hopeful

    hopeful

    Joined:
    Nov 20, 2013
    Posts:
    5,686
    Not sure if you want this, but RAIN provides a free AI, navmesh navigation, and waypoint system.