Search Unity

SimplePath - Advanced Pathfinding for any Game Genre - RELEASED

Discussion in 'Assets and Asset Store' started by alexkring, May 18, 2011.

  1. Goldrake

    Goldrake

    Joined:
    Feb 6, 2010
    Posts:
    148
    I looked at the code and i can change some line of code on the IHeightmap SimpleHeight method.
    Can you suggest me a line of code to calculate the hieght of the mesh from input grid position?
     
  2. runner

    runner

    Joined:
    Jul 10, 2010
    Posts:
    865
    Someone else posted this code in the thread, I managed to insert the code so that the agent turns and faces the direction along the path though somewhat in clunky manner as it leans forward in the direction of travel, Maybe i will suss this out or maybe not.

    perhaps in some future update you might see in you're heart too include this feature request.

    Thanks
     
    Last edited: Jun 28, 2011
  3. alexkring

    alexkring

    Joined:
    May 5, 2011
    Posts:
    368
    There's a couple of ways you could do this. I think the best way, is to generate some sort of heightmap offline, and then just do a lookup into that array at runtime. In other words, you should have a process where you export a level, and that writes out the heightmap data into an array, and then at runtime you can lookup into that array to determine the height of a particular location.
     
  4. alexkring

    alexkring

    Joined:
    May 5, 2011
    Posts:
    368
    I have this in my local version of SimplePath. I'll just give you the code directly right now. I do plan on including this in the future, I'm just waiting to add a few more features.

    Right after the line of code "rigidbody.velocity = newVelocity;", add the following code:

    Code (csharp):
    1.  
    2. // Turn
    3. Vector3 desiredLookPos = seekPos;
    4. desiredLookPos.y = transform.position.y;
    5. Vector3 desiredLookDir = desiredLookPos - transform.position;
    6. desiredLookDir.Normalize();
    7. float rotationDeltaInRadians = m_rotationSpeed * Time.deltaTime;
    8. Vector3 newForwardDirection = Vector3.RotateTowards(
    9. transform.forward,
    10. desiredLookDir,
    11. rotationDeltaInRadians,
    12.  0.0f);
    13. transform.forward = newForwardDirection;
    14.  
    And you should add a public variable called m_rotationSpeed, to this component. This value will control how fast your agent rotates. The agent will only rotate in the XZ axis (he wont tilt forward or backward).
     
    Last edited: Jun 27, 2011
  5. runner

    runner

    Joined:
    Jul 10, 2010
    Posts:
    865
    Thanks a bunch! alex, that did the trick. :)
     
  6. HJP

    HJP

    Joined:
    Jul 8, 2009
    Posts:
    152
    Yes, that is much much better. Thanks
     
  7. HJP

    HJP

    Joined:
    Jul 8, 2009
    Posts:
    152
    Add this to your code:

    // The actor is not falling
    if (!(((int)(newVelocity.y)) < 0))
    // Move the actor on the path
    rigidbody.velocity = newVelocity;
     
    Last edited: Jun 28, 2011
  8. PixelFish

    PixelFish

    Joined:
    May 4, 2011
    Posts:
    12
    Wondering if anyone has had any success using a static mesh instead of the Unity Terrain for uneven terrain and heightmaps?
     
  9. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,368
    Hello, ^^
    I just bough your Simple Path system. And I'm already having errors with:
    I get non stop errors, like this:
    vertexCount > 60000
    UnityEditor.DockArea:OnGUI()

    Is making Unity reaaalllly slow( and most time making Unity crash), can't even select or do anything.
    I have one grid (PathGridWithObstacles). With 46 rows and 28 columns, Cell size is set to 0.25f.
    How is that possible that the vertex count is higher than 60000? I mean, the grid is only 46*28. :|
    Btw, i did that test with a very small level (a room), higher levels will provably blow out. ^^
    Do you know what could be the cause of that error?
     
    Last edited: Jun 29, 2011
  10. alexkring

    alexkring

    Joined:
    May 5, 2011
    Posts:
    368
    This is from the debug lines displaying the grid in the scene view, it won't cause any problems with your game. You can untick the "Debug Show" box on the PathGridWithObstacles prefab (on the PathGridComponent) to make this warning go away.
     
  11. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,368
    Forgot to mention it, yeah i figured it out. ^^
    Tanks anyway for your post. :)
     
  12. bgivenb

    bgivenb

    Joined:
    Sep 15, 2010
    Posts:
    449
    I wish I could try this out, but the asset store glitched up on me; I bought it for $80, completed my purchase, got my receipt, but it says that I haven't bought it (my paypal sent out the money). Its kind of pissing me off that Unity wont answer my e-mails.
     
  13. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,368
    Alex,
    I'm a bit worried, why is ObstacleGridComponent taking too much ressources?
    Image:
    $dd2b7e910eb6fe605ca7d3c7418b09fb76e2f84ea9.png
    I have a tiny grid with only one agent moving.
    Btw, I've disabled the grid debug (to avoid the vertex > 60000 error).
     
  14. alexkring

    alexkring

    Joined:
    May 5, 2011
    Posts:
    368
    This component is responsible for rasterizing the FootprintComponents into the grid. In other words, this is what tells the grid what is blocked. This gets updated every frame, by default. If the obstacles with footprints in your game don't move, you can simply uncheck the "rasterize every frame" box, and it will only rasterize on start, and thus takeup a much smaller amount of resources. If you do this, then you are responsible for determining when is the best time to update the obstacle grid, by calling Rasterize() on this component yourself. If you need this to be faster and the obstacles in your environment are very dynamic, then you will need to write some code to do something like re-rasterize only the cells that are affected by a moving obstacle.
     
  15. runner

    runner

    Joined:
    Jul 10, 2010
    Posts:
    865
    For what its worth this is my profile using the defaults in a scene

    $Profiler2.jpg

    If you turn off debug in SimplePath in the Inspector at 3 locations performance is vastly better
     
    Last edited: Aug 3, 2011
  16. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,368
    It would be nice to rasterize in a per "obstacle" basis. Instead of rasterize all obstacles every frame, as 95% of my obstacles are statics they don't move, only few of them are dynamic and need to be rasterized every frame, so actors can avoid them correctly.
     
    Last edited: Jun 30, 2011
  17. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,368
    It looks like is taking lots of ressources too on your side.
     
  18. PixelFish

    PixelFish

    Joined:
    May 4, 2011
    Posts:
    12
    Other than using a raycast and aligning to its normal, does anyone have a better way to tilt the character forwards and backwards along an uneven mesh?
     
  19. djinni69

    djinni69

    Joined:
    May 10, 2010
    Posts:
    174
  20. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    That's probably the best method.

    You are aware that human beings do not tilt backward to align with the ground when we walk up hills though, right? We would fall over.
     
  21. PixelFish

    PixelFish

    Joined:
    May 4, 2011
    Posts:
    12
    Thanks for the reply. Yeah I should have mentioned my characters are animals.
     
    Last edited: Jun 30, 2011
  22. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    Ahh, that makes sense now :)
     
  23. Logistic-Win

    Logistic-Win

    Joined:
    Apr 4, 2009
    Posts:
    107
    Is there a simple way to get SimplePath to automatically play an animation when the player object is made to move (I have a Walking animation I want to run while the pathfinder has my character move), and then stop when the player arrives at the destination? I am using the Chase script.
     
  24. alexkring

    alexkring

    Joined:
    May 5, 2011
    Posts:
    368
    No, there's no built-in support for this. If you want to add animations, I think it's best to put that logic in the interaction scripts.
     
  25. Logistic-Win

    Logistic-Win

    Joined:
    Apr 4, 2009
    Posts:
    107
    Where would you suggest that I intercept your code? I need to Play when walking starts, Stop when walking ends.
     
  26. mrbdrm

    mrbdrm

    Joined:
    Mar 22, 2009
    Posts:
    510
    have you implement the two floors future yet ?
    i mean in buildings where you have more than one floor and a stair connecting them .
     
  27. alexkring

    alexkring

    Joined:
    May 5, 2011
    Posts:
    368
    Inside the interaction. The interaction code is just there to demonstrate an example of how you can interface with SimplePath. I think you should create your own interaction. You can see where navigation starts and ends in one of the example interactions
     
  28. alexkring

    alexkring

    Joined:
    May 5, 2011
    Posts:
    368
    I don't plan on adding support for this.
     
  29. Logistic-Win

    Logistic-Win

    Joined:
    Apr 4, 2009
    Posts:
    107
    In which script(s)?
     
  30. game2com

    game2com

    Joined:
    Sep 10, 2010
    Posts:
    9
    hi. i want rotate my ai to target. i use this code
    Code (csharp):
    1. function RotateTowards (positions : Vector3)
    2.     {
    3.    
    4.         var direction = positions - transform.position;
    5.         direction.y = 0;
    6.         if (direction.magnitude < 0.1)
    7.             return;
    8.        
    9.         // Rotate towards the target
    10.         transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
    11.         transform.eulerAngles = Vector3(0, transform.eulerAngles.y, 0);
    12.     }
    i Inactive rigidbody.constraints.FreezeRotationY.
    Then with up code rotate ai to target but i have problem.
    when ai go to next target, ai rotate to target but move forward,
    please get me new code to rotate ai to current path or node.
    sorry for my bad english.
    image :
     
  31. alexkring

    alexkring

    Joined:
    May 5, 2011
    Posts:
    368
    The example interaction scripts are interaction_chase, interaction_wander, and interaction_patrol
     
  32. Logistic-Win

    Logistic-Win

    Joined:
    Apr 4, 2009
    Posts:
    107
    I love the plugin... but it's useless to me if the characters I use it for aren't animated. Has anyone else that has used this prefab successfully implemented animation queue's for it? I have tried adding animation start and stop commands in with SimplePath to no avail... The same code worked fine with the Character Controller... but not with SimplePath.
     
  33. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,368
    I don't think you'll get much support for such thing as it have nothing to do with pathfinding.
    But here is:
    Code (csharp):
    1. //Playing and stoping an animation is as simple as:
    2. animation.Play();//assuming that you already have something in your default animation slot.
    3. //and
    4. animation.Stop();//assuming that you already have something playing.
    5.  
    6. //or if you a simple way to blend animations:
    7. animation.CrossFade("walk");//when your agent is walking
    8. //and
    9. animation.CrossFade("idleOrStopAnim");//to blend to a non moving animation
    Those are quite trivial. I guess you are new to unity? I recommend you to check out the animation tutorial on the Unity ressource web site.
     
    Last edited: Jul 5, 2011
  34. Logistic-Win

    Logistic-Win

    Joined:
    Apr 4, 2009
    Posts:
    107
    Actually, I'm not new... I've used the same commands in the other pathfinder I was using before, but like I said... transferring that code (the same commands you just responded with) are for some reason NOT working (probably because the .Play command is being fired over and over again when I need it to just fire once at the beginning of the path). I had originally put the animation.Play command where I THOUGHT your code would trigger it ONCE for the beginning of the path movement... and the animation.Stop command where I THOUGHT it would fire ONCE when the path was completed. But if you remember my original question, I had asked for your knowledge of where to put these commands (so they would be fired correctly with the start and finish of the path to follow). I KNOW how to program the animations, BUT I don't know where in your code I can put these commands without the .Play command getting run multiple times instead of ONCE like it should be when a path to traverse begins. Am I making sense to you? If not, please let me know and I will try to be more clear.
     
  35. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,529
    does this have functionality if I want to constantly update a position based on KeyDown from mouse2?

    The objective would be to be able to right click and hold then the object begins moving and i can continue updating its destination by moving the mouse around with right click pressed.

    I have a script that does this but it does not work with physics so i can fly through kinematic objects, which is not good. I'm not particularly interested in the pathfinding aspect of this, mostly just being able to interact with physics objects and colliders, send/rcv force from other speeding objects and so forth.
     
    Last edited: Jul 6, 2011
  36. seon

    seon

    Joined:
    Jan 10, 2007
    Posts:
    1,441
    Zerobounds: Yes, using the chase interaction example, if you keep moving the chase object position, the item chasing is will continue to re-pathfind to get to it. I use a similar approach for telling my units in my game where to go and can change their destination whenever I want.
     
  37. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,529
    ok so i could still ray cast with mouse to stream updates in and use the hitpoint as the chase target
     
  38. seon

    seon

    Joined:
    Jan 10, 2007
    Posts:
    1,441
    Yes, or have a target gameobject that is invisible that the item follows and you position it at the ground where your raycast hit. I use this approach, but not with an invisible object, but rather a marker object so the player can always see an items destination point.
     
  39. HJP

    HJP

    Joined:
    Jul 8, 2009
    Posts:
    152
    Last edited: Jul 10, 2011
  40. alexkring

    alexkring

    Joined:
    May 5, 2011
    Posts:
    368
  41. alexkring

    alexkring

    Joined:
    May 5, 2011
    Posts:
    368
    AiGameDev.com just posted an article on SimplePath: It provides some of the sales figures, and my experience creating SimplePath.
     
  42. bryanleister

    bryanleister

    Joined:
    Apr 28, 2009
    Posts:
    130
    I am developing an iOS game as well and was wondering your advice on the best approach to keep path calculations to a minimum. Is it best to have a large number (100) of enemies placed and then activate their pathfinding when the player is near, or would it be better to only have 10 enemies that are moved as the player moves through the path grid?

    My path grid is about 250 X 20, the player starts at one end and moves towards the other end.

    If I only have 10 enemies, I might switch to a wandering agent that picks a random spot not more than X grid units from where they currently are.

    If I go with patrol agents, the patrol nodes could be fixed ahead of time. Would you recommend I de-activate and activate the Interaction Patrol script based on proximity to the player, or change the Replan Interval to 0? Other options?
     
  43. alexkring

    alexkring

    Joined:
    May 5, 2011
    Posts:
    368
    I understand that your goal is to maximize performance, but I don't quite understand the options that you have explained. Can you re-phrase or explain these in more detail? And maybe describe more about your game?

    It sounds like it would be easy to test out all of these configurations. You could just try one of the options you described, and see if performance is okay. If the performance is fine, then you don't have to worry about making any optimizations. But if you can describe your problem in more detail, I can offer better advice.
     
  44. bryanleister

    bryanleister

    Joined:
    Apr 28, 2009
    Posts:
    130
    I am doing tests to see what works, and right now if I enable either the enemy agent gameobject or enable the InteractionPatrol script during the game, I see a big spike in the Profiler indicating that the PathManagerComponent is using up to 98% of CPU usage for an moment. I'm afraid this could translate to a glitch in the game play.

    What I'm trying to do, and have done in past projects is to limit the number of enemies to 10. Think Space Invaders, When an enemy falls off screen, I script the enemy to reappear above the player and it falls again. To the player, this feels like an unlimited number of enemies, even though I might only have 10 enemies in the game.

    For this game, I have a large space with obstacles, like a maze. I would like to set up enemies to either patrol, or move around on the grid near the player, but feel that I would only want a limited number patroling at any given time. Why waste processing power on enemies that are near the end of the space and the player can't see?

    The player can move quickly, however, so I want the enemies to be patrolling near the player so the player approaches them and tries to avoid them. I'll have an attack/chase state when the player is very close.

    From my tests, it seems most efficient to simply change the patrol nodes for the enemies. But, if my player moves quickly, there could be a scenario where the enemy must plan a path from too far away, creating an error or requiring me to increase the max number of nodes per planner.

    It takes me a bit longer to script these things, so I was just wondering if there is a typical approach to activating/de-activating path finding on the agents.
     
  45. alexkring

    alexkring

    Joined:
    May 5, 2011
    Posts:
    368
    Ah I see. Yea, I think it's a good approach to not have units that are super far away pathfind toward the player, since that will save CPU time in your case. Though I am curious about the spike you are seeing. If you can give me more details about it, I can look into optimizing it.
     
  46. bryanleister

    bryanleister

    Joined:
    Apr 28, 2009
    Posts:
    130
    Maybe the spikes are unavoidable, as looking into it the spikes are when A* planner is working, which makes sense if a new agent is activated. Right now, my best performance seems to be setting the agents Replan Interval to 100 and then activating the Patrol Script when the player is near.

    Attached are some screen shots, titles indicate what I was doing in the Editor:
     

    Attached Files:

  47. alexkring

    alexkring

    Joined:
    May 5, 2011
    Posts:
    368
    Thanks for the screenshots, I'll look into this.
     
  48. HJP

    HJP

    Joined:
    Jul 8, 2009
    Posts:
    152
    Nice article, Alex ;-). Bye the way, I am a Programmer :) .
     
    Last edited: Jul 11, 2011
  49. flim

    flim

    Joined:
    Mar 22, 2008
    Posts:
    326
    Nice article, but I do not agree Unity user don't want to program, I am a programmer. Unfortunately I read some A* articles but still not understand the trick, and 3D Buzz don't want to sell the AI programming course to me. I purchase SimplePath because I didn't know the trick of A* yet. Actually it would much more worth to me if someone create a STEP-BY-STEP tutorial how to implement A*.
     
  50. bryanleister

    bryanleister

    Joined:
    Apr 28, 2009
    Posts:
    130
    I read the article too, nice information and great to hear from your side how things are going. I think you'll find a mix, but yes, I would imagine most people paying for add-on functionality are doing so either to save time or because they don't want to write code. For me personally, I could probably scratch something together, but I am so focused on performance that I prefer to go with something written by a person with your experience.

    I have about 12 agents working in my iOS game and the performance seems fine, even on an older iPhone 3G. I'm finding that may be enough for my game, but still, if there was a way to prioritize or deal with agents far from the main character, that would be great. For example, you expose a 'max number of Planners'. It would be great if the max number could be prioritized so that the planners far from the action would quit planning, while the planners near the player would have priority.

    That way, I could limit my max number of planners to 10, but go ahead and place 30 or 40 in my scene, knowing that they would not be doing anything until the player approached.