Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Is there any pathfinding plugin for 2D platformer game with gravity but no jumping feature?

Discussion in 'General Discussion' started by Solidcomer, Aug 11, 2021.

  1. Solidcomer

    Solidcomer

    Joined:
    Sep 12, 2017
    Posts:
    95
    I've tried to adapt A star to a path-finding feature similar to what we can see in the commercial game Craft The World or Oxygen Not Included. It is a 2D tile-based platformer game where the characters have gravity, they can climb wall and ladder, but they can NOT jump.
    Many solutions and tests failed, I'm wondering if there's any plugin that could do this, or is there any article illustrating any solution?

    Thanks very much.
     
    Last edited: Aug 11, 2021
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,574
    Which solution failed and why?
    A* for 2d is probably most trivial of all pathfinding approaches. Specially 3d.
     
  3. Solidcomer

    Solidcomer

    Joined:
    Sep 12, 2017
    Posts:
    95
    Thank you for the reply.
    I've uploaded a screenshot of the scene in Unity Editor, this is a case where A* can't solve the problem.
    Tiles marked with red dots in the scene are not walk-able, and the rest ones are walk-able. And the game has gravity on the character, which prevents him from moving upwards without ladders.
    When the game runs, the blue character controlled by A* is supposed to move along the blue path in order to reach the star item, this is the path a human player will choose if the player can control the character, because there's a ladder on the path, so the character can reach the star by climbing the ladder, and then get to the higher platform. But A* will drive the character to move along the green path, because A* doesn't know the character has to stay on the ground due to the gravity.
    But of course, if tiles on & alongside the green path are all marked as not walk-able, A* will choose the blue path, but according to the design of the game world, these tiles should be walk-able, e.g. the character should be able to get down from the higher platform to the lower ground via the path.

    I've read the A* documents, it says the algorithm itself can't deal with platform game like Super Mario, not to mention, my character can't even jump.
    So I don't know how to deal with it.
     

    Attached Files:

  4. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,321
    The problem is with the data you fed into the algorithm, and not with the algorithm itself. Tiles above ground that do not have ladders on them should be marked as non-walkable.

    Now, that still won't allow you to deal with purposedly dropping off a platform.or jumping. To implement jumping off ledges/cliffs you'll need to implement "teleport points" which can be one way and then see if this can be shoehorned into A*.
     
    Antypodish likes this.
  5. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,574
    Teleporting may be resealable solution. But adds complexity.

    Another simpler way, is weighted cells,
    So basically applying cost for each cell. Up may be for example lower cost, than moving forward.
    Then A* algorithm will try to find cheapest route.
    So if you can jump 3 cells high, apply cost of 3 for each cell ahead, and cost of 1 for each cell above.
    This will prioritise jumps, over just walking.
    Also, you can apply cost to ladders.
    Or any arbitrary area. I.e. poison. You may want to allow to go through it, if there is no other path available.
     
    Last edited: Aug 21, 2021
  6. EternalAmbiguity

    EternalAmbiguity

    Joined:
    Dec 27, 2014
    Posts:
    3,144
    That just looks like a restricted-direction path to me.
    grid.png
    Cells not on the ground (you can check for (x,y-1)) - (2,3), (2,2), and (2,1) can only be entered from the sides or above, and the entity will fall when it enters the cell (if it were a player you could prevent control or limit it to horizontal movement).

    So when your A* hits that cell, which denotes that it's in the air, it should check if the previous cell was above or to the sides.
     
  7. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    968
    The input to A* is generally a graph, that has nodes and edges. The nodes are your (walkable) tiles, and the edges are the (walkable) connections between the tiles.

    So when you construct your graph, for a given node (tile), just add edges to the other tiles that are actually reachable. So for a ground tile, you would add an edge to the right and left, but not up because you can't jump up in the air. But for a tile that's up in the air, you can add an edge down to the tile below because you can (presumably) fall.

    Then you just run the A* algorithm on this graph.