Search Unity

Unity2D Turn-based pathfinding

Discussion in '2D' started by bobatismo, Jun 12, 2014.

  1. bobatismo

    bobatismo

    Joined:
    Jun 11, 2013
    Posts:
    11
    TL;DR: I want to have a bot move toward the player. But my game operates in turns. There are obstacles. It's on a grid where anyone can move up/down/left/right. While I can make bots go up/down/left/right randomly, or scripted-ly, I can't figure out how to do it based on where the player or some other waypoint is. Help. :)

    The long version:
    I'm stumped on 2D, top-down, turn-based, maze/grid-like pathfinding and was hopeful someone would have some insight or suggestions.

    I'm working on something similar to old RPGs like Ultima III or IV. The game only progresses when you perform an action. If it helps, here is a video of my project, so far, in action:


    I'm trying to create a "tracking" system. As the player moves, he'd (unknowingly) lay down (invisible) waypoints that would increment. The "tracker" AI bots would prioritize, in order: the player (but only within a specified range); the closest waypoint of any value (but only within a specified range); or, if the aforementioned cannot be found, the lowest-numbered waypoint that they have not already visited.

    I have a fair bit of this working. I can gauge distance between any two objects. I can make a tracker move in a specific direction or randomly. I can drop invisible waypoints that increment. I can set the current state for the waypoint the tracker should be moving toward. I can have the raycast2d check for line of sight in case something like a wall is blocking the view of the player. But I can't figure out how to move the tracker in the correct direction.

    I was thinking that maybe I could figure the vector2.angle between the tracker and the player, and if it was, say, 45 degrees, try to move up or to the right, whichever was available after a raycast2D check. If neither, then just move randomly and try again next turn based on the current angle (or maybe drop more waypoints to create a trail of bread crumbs). But when I hooked that up I was getting weird angles, like 91 degrees when I'm one unit over and three units down from the tracker, or 18.6 degrees when I am directly next to it (to the right).

    If you've got some thoughts on what I'm doing wrong with vector2.angle, or if you think I am going about the tracking function the wrong way, please let me know. I could use a shove in the right direction. :)

    PS I am sure there are a great number of pre-made solutions available. I'm doing this to learn C# and Unity basics, so I intend to avoid those for now. :)
     
    Last edited: Jun 12, 2014
  2. bobatismo

    bobatismo

    Joined:
    Jun 11, 2013
    Posts:
    11
    Thanks to this post, I found part of my problem. This is good to know, but doesn't get me closer to working toward a solution:

    aldonaletto wrote "Vector2.Angle(v1, v2) assumes that v1 and v2 are vectors, not positions - think of them as arrows based on (0,0) and pointing to v1 and v2. In screen coordinates, if v1 is at the top-left and v2 is at bottom-right, Vector2.Angle(v1,v2) will return 90 degrees; if your fingers are in the same line from (0,0), it will return 0 degrees, because the 2 vectors will point the same direction (the length, which is the distance from screen origin, doesn't interfere with the angle).

    It would be better in this case to use Vector2.Distance(v1,v2), because Distance assumes that v1 and v2 are positions - it will accurately report a increasing value when the fingers are getting away from each other, and a decreasing value when they're getting closer."
     
    Last edited: Jun 12, 2014
  3. bobatismo

    bobatismo

    Joined:
    Jun 11, 2013
    Posts:
    11
    I thought I would bump this once just in case some people may have missed it, but have some thoughts on it. I'm even willing to simplify things even further. I was thinking I could do something like having a bot just walking in a set pattern, because I can manage that. But I want him to approach the player if the vector2.distance between him and the player is less than a number of units. I can do the latter part, but I still can't figure a way for him to know which direction the player is from him. If I knew that, I could get him walking toward the player. Any thoughts before I let this one die? :)
     
  4. Valette

    Valette

    Joined:
    Jun 9, 2014
    Posts:
    68
    I needed something like that, turning a sprite in the direction of an object and this is the code I use based on an answer I found in the distant past somewhere on the web. Hope it helps

    Code (CSharp):
    1. Vector3 direction = mytarget.transform.position - mycurrentpos.transform.position;
    2. float angle = Mathf.Atan2 (direction.y, direction.x) * Mathf.Rad2Deg;
    3. mycurrentpos.transform.rotation = Quaternion.AngleAxis (angle, Vector3.forward);
     
    bobatismo likes this.
  5. bobatismo

    bobatismo

    Joined:
    Jun 11, 2013
    Posts:
    11
    Hm, interesting! And thank you! I would say more but I am still processing what you've written. I think I need to experiment with it a bit before I really get it. But thank you so much, that is very, very helpful! :)
     
  6. Storyteller

    Storyteller

    Joined:
    May 15, 2012
    Posts:
    23
    look up A* (A Star)

    its a very old, tried and true pathfinding algorythm.
     
  7. bobatismo

    bobatismo

    Joined:
    Jun 11, 2013
    Posts:
    11
    I have found some good documentation on write-ups on A*, and it looks to be very helpful. Thank you for the comment. :)
     
  8. WidmerNoel

    WidmerNoel

    Joined:
    Jun 3, 2014
    Posts:
    39
    For everybody that has problems to understand the different kinds of vectors:
    Go to youtube.com and search for "math for game dev"
    You'll find Jorge R. He has tutorials on how to understand vectors, quaternions, radians, etc...
    Helped me a lot lately!
     
    bobatismo likes this.
  9. bobatismo

    bobatismo

    Joined:
    Jun 11, 2013
    Posts:
    11
    Awesome! Thank you! :D
     
    WidmerNoel likes this.
unityunity