Search Unity

2D Pathfinding help

Discussion in '2D' started by ElAshtonio, May 1, 2016.

  1. ElAshtonio

    ElAshtonio

    Joined:
    Jul 18, 2014
    Posts:
    2
    Hi, I'm interested in integrating pathfinding into a 2D rpg game I'm making. Now, I don't really want to just use an asset that someone has made; I want to learn how to do it myself :) So, where would I go about learning how to do this with Unity specifically? I know how to make a pathfinding/search algorithm (I had to trundle through a D1 module in Further Maths AS) but I just don't know how I would implement it into Unity. It's nice and easy to write it in Pseudocode ("Compare the distance from the objects current position to Node A and Node B") but it's a lot harder to do that when you're creating worlds with 1000s of tiles that each act as separate nodes :/

    Thanks!
     
  2. Kazen

    Kazen

    Joined:
    Feb 17, 2015
    Posts:
    68
    So each of your tiles are "nodes" right? In that case, you should know which ones are passable and not. Look up A* or Astar path finding algorithm for how you would make effective pathfinding. Create a script that contains all the functionality and send in your tiles/nodes for reference. If implemented correctly, you can tell the algorithm to say "hey, I'm at node 0, I want to go to node 3298", and the algorithm finds the "path", each node it has to pass through to get from node 0 to 3298. Then it's just a matter of moving your object towards the node position, and when your object gets close to it, change to the next one in the list until you're done.
     
  3. Kurdiez

    Kurdiez

    Joined:
    Feb 6, 2017
    Posts:
    72
    @ElAshtonio I have implemented just that for our game. Hopefully my video of it could help you out. You are free to use any code I have in the video or in any other videos.

    A*Pathfinding done in Unity