Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[Released] BFGames: Simply A* Pathfinding! NOW FREE

Discussion in 'Assets and Asset Store' started by BFGames, Dec 28, 2012.

  1. DanarKayfi

    DanarKayfi

    Joined:
    Aug 27, 2012
    Posts:
    72
    im sorry i didnt know its a unity question cause i tried everything almost even the code u send...what i want is to rotate to face the next red box position....imagine a tower defence game...the enemy will rotate as the road go till he reach the final destination..

    i used this code after line 49 in pathfinding.cs file
    transform.Rotate(Vector3.RotateTowards(transform.position, Path[0], Time.deltaTime* 10, 0));
    but didnt work
    and i used this too in the same place:
    transform.LookAt(Path[Path.Count-1]);
    didnt work too.

    Note: its a 2D TowerDefence on X Y

    Update: i think the best question will be how to know what the next Waypoint Node position is?

    anyway i will understand if you didnt replay cause maybe its not related to ur asset..
     
    Last edited: Jun 21, 2013
  2. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    It is no problem. Ask away!

    You dont want to look at last Vector3 but the first in the Path list.
    So you want to do something like

    Code (csharp):
    1.  
    2. Vector3 direction = Vector3.zero; //We will save this so we keep looking at the last givin direction
    3.  
    4. void RotationHandler()
    5. {
    6.       if(Path.Count > 0)
    7.       {
    8.            //we ignore the Y axis, more or less, change this if you need it to react on the y axis
    9.            direction = (new Vector3(Path[0].x, transform.position.y, Path[0].z) - transform.position).normalized;  
    10.       }
    11.      
    12.       //Look from your position to your position + the direction we are looking at.
    13.       transform.LookAt(transform.position + direction * 10);
    14. }
    15.  
    16.  
    Put this somewhere in update or something like that. If you need it to turn around smoothly you need to use some sort of interpolation.
     
  3. DanarKayfi

    DanarKayfi

    Joined:
    Aug 27, 2012
    Posts:
    72
    Awesome Thanks a lot i think it is working now but not perfectly and i know now why!!! simply because 2D toolkit is not working on xz plane it only works on xy plane...
    im trying to do some modification so it only rotate the z axis so it will work perfectly...
     
  4. johny

    johny

    Joined:
    Aug 31, 2011
    Posts:
    133
    Hey bf games. I have it set up and running correctly using the sample from the pdf documentation but when I instead try to make it so the vector3 location is sent from a camera (one that uses a ray cast and sends the hit.point to the GO with the pathfinder) it just starts sinking into the ground and falling. Any ideas?

    Edit: Either that or if I just edit the default gridPlayer script to accept a raycast position from a camera instead of within the script itself even if it have >0 paths it wont move at all.
     
    Last edited: Jul 9, 2013
  5. BTStone

    BTStone

    Joined:
    Mar 10, 2012
    Posts:
    1,422
    Hey there, I am interested in your Plugin, but i have to know some things first:

    - is your solution mobile-friendly regarding the performance?
    - I'm working on an 2D Platformer (the character walks the X-Axis along and can jump and fall the Y-Axis along), so are these scenarios possible with your solution:
    - some of the Enemies should chase the Player, if they reach an end of a plattform, they should stop. (While Patrolling, Chasing and Attacking the
    Enemies are playing 2D Toolkit Animations <-> would that be compatible? )
    - other Enemies can chase the Player over obstacles, meaning: they can jump over pits, too.

    Thanks in advance for your answers :)
     
    Last edited: Jul 11, 2013
  6. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    First of sorry for the later answer, but been on a weeks vacation.

    The reason why it sinks, is probably that when you just give it the position you gain from the raycast you get the y-value from the ground. If you chars pivot point is, let say in the middle of the character, and you walk towards that point, then it will sink in with half of the size.

    You can fix this by adding a given y-value to the movement or use rigidbody/charactercontroller physics to control the movement.

    I hope this helps
     
  7. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    It is mobile friendly performance wise and works with 2DToolKit (triggering animations is not a part of this package and will not be as it is to specfic for each game).

    But, as you are building a plat former i wouldn't use A* pathfinding at all. You will usually use something else for that.
     
  8. BTStone

    BTStone

    Joined:
    Mar 10, 2012
    Posts:
    1,422
    Allright, thanks for the reply! :)
     
  9. Copywright

    Copywright

    Joined:
    Apr 17, 2012
    Posts:
    21
    Hey,

    Right now, I'm using waypoints for my AI. Just for reference, here it is here. I want to implement local avoidance amongst the AI, so that they avoid going through each other.

    The problem with using Pathfinder2D for me is that the "walkable" area for my game are those without any colliders, and that all characters are z-sorted by setting z = y every frame. When I setup the prefab in the scene, the sidewalk area turns out red. Would there be anyway to keep my sorting method and use A* on the area w/o colliders as walkable?
     
  10. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    The whole system is build up around colliders, so sadly it is not possible with Simply A*.
    But you could maybe just add a collider by "hand", and make it ignore all other colliders?

    Obstacle avoidance is something completely different, but there is a good reference here: http://www.red3d.com/cwr/steer/Obstacle.html

    I might at some point try and add it for different scenarios (the grid scene kinda uses it).
     
  11. SoBiT

    SoBiT

    Joined:
    Dec 5, 2012
    Posts:
    44
    1. Does the grid system support multiple levels? (I have a house with 3 floors)
    2. Does the grid regenerate each update()?
    3. Are all types of colliders avoided? (I'm, making a FPS where pretty every object is movable)

    Thanks in advance!
    SoBiT
     
  12. GiantGrey

    GiantGrey

    Joined:
    Jul 2, 2012
    Posts:
    266
    Hi,

    your system looks really cool and simple :)
    just one question:
    would this work on a sphere like terrain? example if you have one start point on the south pole and the end point is on the north pole... would the agent move trough the sphere because its the shortest way or can it move around the sphere?
     
  13. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    1. No, only the waypoint system does so.

    2. No, but it is able to dynamically change the map based on moving objects, however its heavy with too many updates.

    3. Not sure what you mean with all type of colliders. A collider is more a less a collider? Do you mean mesh collider, box collider and so on? It does not look at it that way, in this system a collider is a collider, and you define which colliders it should use for the map and which to ignore. (by tags)
     
  14. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    With the waypoint system it can move around the sphere, but you need to manually set all waypoints and connect them. The grid based system wont work in this situation.
     
  15. Chio

    Chio

    Joined:
    Jul 19, 2013
    Posts:
    4
    Hey, i'm working on a 2d topdown game using 2d toolkit. I'm using the Pathfinder2D.cs and its working fine, i just have one little problem. I have multiple sprites using colliders, all of them are moving, but when 2 of them collide one of them changes its Z position and basically walks over the other one. Is there any way to change this and make them actually collide?
     
  16. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    Well collision is not really something that is a part of the pathfinding. That is a Unity related subject more of. Look into rigidbodys or char controllers ;)
     
  17. Chio

    Chio

    Joined:
    Jul 19, 2013
    Posts:
    4
    I'm sorry i probably should have explained it better.

    The problem i'm having is that when i'm using the pathfinding scripts to move my sprite,they move like intended but they always move on the Z axis a tiny bit (only about 0.5), causing the colliders to not contact. When i move my sprite from any other source, the sprite only moves on the x and y axis like its supposed to and the sprites collide like intended. Do you think there is anything in the script that could cause this, or could it be something else?

    Thanks for the super fast reply btw :)
     
  18. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    It builds the map based on the colliders. So it will place a "tile" at a colliders Z-depth. If you move (and havent created your own movement) to that and it is on a different Z-value then they will move down to that. You need to ignore the Z axis in your movement.

    If you use the simple test move function in Pathfinding2D.cs then you can change it to this:

    Code (csharp):
    1.  
    2.  
    3.  //A test move function, can easily be replaced
    4.     public void Move()
    5.     {
    6.         if (Path.Count > 0)
    7.         {
    8.             //Add this line ignoring Z!
    9.             Vector3 ignoreZ = new Vector3(Path[0].x, Path[0].y, transform.position.z);
    10.             transform.position = Vector3.MoveTowards(transform.position, ignoreZ, Time.deltaTime * 30F);
    11.             if (Vector3.Distance(transform.position, Path[0]) < 0.4F)
    12.             {
    13.                 Path.RemoveAt(0);
    14.             }
    15.         }
    16.     }
    17.  
    18.  
     
    Last edited: Jul 19, 2013
  19. Chio

    Chio

    Joined:
    Jul 19, 2013
    Posts:
    4
    Oh that makes sense. Thanks a lot. :)
     
  20. Chio

    Chio

    Joined:
    Jul 19, 2013
    Posts:
    4
    I just had 2 more question. Is it possible to use the DynamicTDGridObject with the 2D Pathfindiner and also, is there a way to update the whole gridmap during runtime?
     
  21. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    The dynamic changes is only created for "3D" games at the moment.

    You can call the create map method at runtime to change the map, but i would not do it too often based on performance. If your level is really small it should however not be a big problem (it might conflict with some pathfinding calls though, so i would call it in a coroutine using yield return new WaitForEndOfFrame())
     
  22. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    Update:

    Updated the dynamic grid changes. It can now update a part of the map based on raycasts instead of the object.
    This means that it can now update the map for new walkable areas, such as new bridges over rivers and whatever!
     
  23. AozakiKyuuji

    AozakiKyuuji

    Joined:
    Sep 21, 2012
    Posts:
    15
    Heya,

    i ran into a little trouble. I was following your AI script with the other Pathfinder2D scripts.
    Thing is when I stand still, the enemy smoothly waltz to the Player. But when the Player moves, the enemies starts lagging and barely moving.

    Any idea what is causing this?

    Thanks for the great solution.
     
  24. aegget_

    aegget_

    Joined:
    Oct 30, 2012
    Posts:
    33
    Hello!
    I was just wondering if there is a built in method for excluding waypoints from the path calculation? Right now if a dynamic object stands between the Ai and the desired path, the Ai runs right into it instead of navigating around it. It would be really lovely if I could exclude the desired waypoint by just accessing a boolean on it upon collision with the dynamic object. If I have to write some addon to it can you give me some pointers on how I might go about doing this? I have written an auto path editor addon that auto places and connects pathnodes around the level but that is externally and I dont really want to fiddle with your stuff as that restricts me from updating it.

    Thanks for your answer
     
    Last edited: Aug 16, 2013
  25. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    How often to you recalculate the path for enemies? If you do it too often it might seem to be laggy because it is changeing its path to often as it is looking for a new path because the player moves to a new position.
     
  26. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    Well the dynamic updates are only made for the grid based solution not the waypoint solution.
     
  27. AozakiKyuuji

    AozakiKyuuji

    Joined:
    Sep 21, 2012
    Posts:
    15
    well, I kinda just copied your script, and changed the Vector3.Distance of player and AI to about 800F.
    Since it's the same script as your gridbased scene, and yours did not have the problem, I have no idea what's causing mine.

    So, in which part do I have to make a less often recalculation?
     
  28. aegget_

    aegget_

    Joined:
    Oct 30, 2012
    Posts:
    33
    Ah that's too bad =/ Puts me in a bit of a pickle then because grid solution does not work for me as I have multiple floors and I cant change that unfortunately... Then I'll just have to try to implement that myself somehow =/
     
  29. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    Well it is kind of difficult to implement for the waypoint based solution. An easier approach would be to implement obstacle avoidance in mix with the pathfinding. This is what is done in a lot of games anyways as either pathfinding or steering behaviours is enough in it self in more complicated cases.
     
  30. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    Do you use the ai script from the "3d" grid scene with the 2d solution? That might cause problems. Remember that all AI moving scripts is just examples of how it can be used.
     
  31. AozakiKyuuji

    AozakiKyuuji

    Joined:
    Sep 21, 2012
    Posts:
    15
    Yes, I did... Your tutorial PDF was hard for me to follow as a newbie programmer. I have no idea what is a Path List<> nor know how to implement the Move() method. I probably need more //comments to explain what each command does for me to play around with it.... Sorry >.<

    Code (csharp):
    1. public class EnemyActivity : Pathfinding2D {
    2.  
    3.         public Transform player;
    4.    
    5.     // Update is called once per frame
    6.     void Update ()
    7.     {
    8.         if (Vector3.Distance(player.position, transform.position) < 800F)
    9.         {
    10.             FindPath(transform.position, player.position);
    11.                 if (Path.Count > 0)
    12.             {
    13.                 Move ();
    14.             }
    15.         }
    16.     }
    17.    
    This was the best I could come up with by myself, considering most of the commands are already in Pathfinding2D. And as expected, nothing happens. Guess I'm totally blank on this.
     
    Last edited: Aug 21, 2013
  32. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    I am busy right now, but i can write you a script with comments later today ;)
     
  33. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    So here is a simple 2D AI Script! It will be a part of the new update, but need to fix a few things before updating.

    Just created a new C# script, name it the same as the class (SimpleAI2D), and copy paste it

    Code (csharp):
    1.  
    2.  
    3. public class SimpleAI2D : Pathfinding2D
    4. {
    5.     public uint SearchPerSecond = 5;
    6.     public Transform Player;
    7.     public float SearchDistance = 20F;
    8.     public float Speed = 20F;
    9.  
    10.     private bool search = true;
    11.     private float tempDistance = 0F;
    12.  
    13.     void Start ()
    14.     {
    15.         //Make sure that we dont dividde by 0 in our search timer coroutine
    16.         if (SearchPerSecond == 0)
    17.             SearchPerSecond = 1;
    18.  
    19.         //We do not want a negative distance
    20.         if (SearchDistance < 0)
    21.             SearchDistance = 0;
    22.     }
    23.    
    24.     void Update ()
    25.     {
    26.         //Make sure we set a player in the inspector!
    27.         if (Player != null)
    28.         {
    29.             //save distance so we do not have to call it multiple times
    30.             tempDistance = Vector3.Distance(transform.position, Player.position);
    31.  
    32.             //Check if we are able to search
    33.             if (search == true)
    34.             {
    35.                 //Start the time
    36.                 StartCoroutine(SearchTimer());
    37.  
    38.                 //Now check the distance to the player, if it is within the distance it will search for a new path
    39.                 if (tempDistance < SearchDistance)
    40.                 {
    41.                     FindPath(transform.position, Player.position);
    42.                 }
    43.             }
    44.  
    45.             //Make sure that we actually got a path! then call the new movement method
    46.             if (Path.Count > 0)
    47.             {
    48.                 MoveAI();
    49.             }
    50.         }
    51.         else
    52.         {
    53.             Debug.Log("No player set in the inspector!");
    54.         }
    55.     }
    56.  
    57.     IEnumerator SearchTimer()
    58.     {
    59.         //Set search to false for an amount of time, and then true again.
    60.         search = false;
    61.         yield return new WaitForSeconds(1 / SearchPerSecond);
    62.         search = true;
    63.     }
    64.  
    65.     private void MoveAI()
    66.     {
    67.         //Make sure we are within distance + 1 added so we dont get stuck at exactly the search distance
    68.         if (tempDistance < SearchDistance + 1)
    69.         {      
    70.             //if we get close enough or we are closer then the indexed position, then remove the position from our path list,
    71.             if (Vector3.Distance(transform.position, Path[0]) < 0.2F || tempDistance < Vector3.Distance(Path[0], Player.position))
    72.             {
    73.                 Path.RemoveAt(0);
    74.             }  
    75.  
    76.             if(Path.Count < 1)
    77.                 return;
    78.  
    79.             //First we will create a new vector ignoreing the depth (z-axiz).
    80.             Vector3 ignoreZ = new Vector3(Path[0].x, Path[0].y, transform.position.z);
    81.            
    82.             //now move towards the newly created position
    83.             transform.position = Vector3.MoveTowards(transform.position, ignoreZ, Time.deltaTime * Speed);  
    84.         }
    85.     }
    86. }
    87.  
    88.  
    Remember to add the player in the inspector!
     
  34. AozakiKyuuji

    AozakiKyuuji

    Joined:
    Sep 21, 2012
    Posts:
    15
    Thank you very much!! It's so well commented that I can see possibilities of playing round with it

    My life is complete
     
  35. FriarBabs

    FriarBabs

    Joined:
    Jul 9, 2013
    Posts:
    6
  36. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    Well kind of hard to help as this is created for 2DToolkit which i do not know much about.

    But seems like it is either e a Unity problem with gizmos, as it does a null check for the Map, or you created a Map but did not fill it correctly, so Map[i, j] is null.

    Try and put this check in at line 741 above the gizmo call:

    Code (csharp):
    1.  
    2.    if (Map[j, i] == null)
    3.           continue;
    4.  
     
    Last edited: Aug 22, 2013
  37. FriarBabs

    FriarBabs

    Joined:
    Jul 9, 2013
    Posts:
    6
    That ended the console nagging, which must mean that the map isn't filling properly..ugh.
    Any pointers, starters, ideas? Haha. This is going to be painful.
     
  38. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    Well guess you should ask in that thread, they probably got a better idea when it comes to 2DToolKit.
    Cannot really debug that script for you as i dont have 2DToolKit.
     
    Last edited: Aug 22, 2013
  39. FriarBabs

    FriarBabs

    Joined:
    Jul 9, 2013
    Posts:
    6
    Mmk, thanks for trying anyways. Hopefully I'll get this resolved..
     
  40. FriarBabs

    FriarBabs

    Joined:
    Jul 9, 2013
    Posts:
    6
    I seem to have somehow got the nodes placed?? Though when I make paths, it COMPLETELY ignores the "walkable" flags and also makes them EXTREMELY inefficient, often bringing the player to the bottom of the map and then back up again. This is quite frustrating. I have PM'ed you a link to an example project of this. All you have to do is hit "play" to see the behavior.. Mob one is the object in question. I would really appreciate if you could take a look at this.
     
  41. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    You placed your map in the XZ plane, guess you need to place it in the XY plane. Because i can see that it creates the map in the XY plane with that script. You can see it from using DrawMapInEditor.
     
    Last edited: Aug 23, 2013
  42. FriarBabs

    FriarBabs

    Joined:
    Jul 9, 2013
    Posts:
    6
    Hm. Okedoke. I'll give it a go. Thanks a ton!
     
  43. FriarBabs

    FriarBabs

    Joined:
    Jul 9, 2013
    Posts:
    6
    I sent you another PM with another link.
     
  44. wallaceb68

    wallaceb68

    Joined:
    Jun 20, 2013
    Posts:
    10
    There are inconsistent line endings in the 'Assets/Pathfinding/Scenes/Scripts/Lines.cs' script. Some are Mac OS X (UNIX) and some are Windows.
    This might lead to incorrect line numbers in stacktraces and compiler errors. Many text editors can fix this using Convert Line Endings menu commands.
     
  45. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    Thanks, will fix it for the next update ;)
     
  46. macs054

    macs054

    Joined:
    Jul 28, 2013
    Posts:
    4
    Hello and good day. We are currently planning to make a tactical game using tile-based movement on a grid. We're planning to use A* algorithm and I happen to found this tool of yours. The game we're gonna make is similar to Advance Wars. I just want to ask if this is the perfect tool for us, does this come with a level editor? Or does it make creating the levels easy since the grid is auto generated? I tried the demo and it does diagonal movement which doesn't apply to some tactical grid based games. Can we tweak the code so it does only straight vertical and horizontal movements? Anyway great tool and we are considering to buy this if this is the correct solution for us. Thanks and have a good day.
     
  47. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    The map is auto generated when the scene is loaded. Which makes it easy to use.
    For "3D" games it can even dynamically change the map at runtime.

    There is an option in the inspector which tells it NOT to move diagonal already. So that is a one-click-fix (See the TD test scene where it does not move diagonal)
     
  48. liverolA

    liverolA

    Joined:
    Feb 10, 2009
    Posts:
    347
    Hi, How can i set the node cost,currently the kit only support make the node walkable/unwalkable,but i want to change the node cost only,in my rts game when moving a group of units,i want to use node cost to avoid the units overlap,when a unit stops,the node it stands will change the cost to high,so others will not pass.
    Can you make an small example/code to show how to move group units? Appreciate
     
    Last edited: Aug 29, 2013
  49. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    It is sadly not build to support that you can change the node cost of a single node. There is only a cost for moving diagonal and one for moving straight.

    Also you would normally fix things like that using steering behaviours (flocking) together with an A* algorithm.

    This book got some really nice explanations for how to do stuff like that:
    http://www.amazon.com/gp/product/0123747317/?tag=idmme-21
     
    Last edited: Aug 29, 2013
  50. Polantaris

    Polantaris

    Joined:
    Jun 25, 2013
    Posts:
    18
    Hrm...I may grab this in the near future.

    I'm working on a roguelike, so almost everything will be done with procedural generation. It seems like most of the pathfinding solutions available in the Asset Store do not work with that, however yours seems to. If it can, I'll probably jump on this, although I still don't know if I will be making my roguelike turn based or not at this point, but I need pathfinding either way.

    Would I be able to place the nodes myself in prefabs of objects that would be generated, and then the grid would be based off of that, or how does it work? I already have my generation script in (mostly) very good working order, so I'm not going to redo it.