#Working on making pathfinding in unity

1 messages ยท Page 1 of 1 (latest)

lean onyx
#

Very new to this. but I can actually ask a question now that isn't just "me no get how work anything aaaa"
c: big thanks to @faint wolf and @dreamy wind for what I've managed to do so far.

currently working on iterating backwards through the tiles to get the path. I need to correctly set the weight, yes?
if anyone could direct me to how I can go about setting the 'weight' properly that'd be greatly appreciated.

there was more to this, but I've managed to figure out most of it c:

what it does>
generate a grid
pick point on grid
spread outwards tile by tile checking all 4 directions, ignoring checked tiles and blocked tiles
find and identify point on grid

now I need to get the path back from it. hence I need to set the weight of each tile created, so I can count backwards to get the path. any help is appreciated. thank you.

#

very happy with how this has gone so far.

lean onyx
#

e

olive tartan
#

I think you want to have a weight of 0 and each iteration of spreading you want to increase it by one and assign that weight to the tiles that it spread to.
To find out the path you should be able to go from the finish and pick the tile with smallest weight around and go that way to the start.

#

@lean onyx

#

^That's a very simple sum up from my head. But if you want pathfinding you should look up A*. There is a lot of nice tutorials and articles on it.

lean onyx
#

thank you, that seems to work pretty well. testing it out rn. c:

lean onyx
#

@olive tartan I appear to have messed something up in my logic.
I check all four directions from a tile, and if it hasn't been checked yet, isn't null and isn't a collider,I set it's weight to the initial tiles weight, plus one. then I add it to the checked list.
I also add the start tile to the checked list to avoid it getting extra weight added.

but the result is like so:

middle tile has a weight of 4 by the end, starts with 0..
tile to the left gets a weight of 1
tile to the bottom gets a weight of 2
tile to the right gets a weight of 3
[top tile is a collider so nothing]

I'm not sure what could've caused this? am I missing something lol?

#

I'll provide a snippet [all 4 direction checks are the same as this one] differing only of course in what currx currz get set to



            if (dir == 0) {
                currx = zerox + 1; //zeroz and zeroz are the values of the tile it's checking around
                currz = zeroz;
                t2c1 = "Grid (" + currx + "," + currz + ")";
                Transform TILEA = GridBase.transform.Find (t2c1);
                if (unreachable.Contains (t2c1)) {
                    count = count + 1; //a check to see if it's a collider
                } else { //make sure we're on the grid and not already checked
                    if (currx>0 && currx<10 && currz>0 && currz<10 && TILEA.IsChildOf(GridBase.transform) && !(Checkeda.Contains(TILEA.gameObject)==true)){
                        Checkeda.Add (TILEA.gameObject);
                        TILEA.gameObject.GetComponent<GridBehave>().weight = Tileo.GetComponent<GridBehave> ().weight+1;
                    }
                }
            }```
olive tartan
# lean onyx I'll provide a snippet [all 4 direction checks are the same as this one] differi...

Are there any other places where you set the weight? I don't see any issue here tbh

Also I got some suggestions for your code.

Store the nodes in a 2D array so you don't have to make strings, do object finds , checking strings, etc.. You could put it to this script and initialize it on awake.
You would have a public Vector2 on the GridBehave node and in Start/Awake of GridBase you would do a search for all GridBehaves and assign them to the array by their vectors.

I'm a little bit confused why are you storing unreachable nodes as strings and checked nodes as gameobjects.

lean onyx
#

I can't find anywhere else that I set the weight :L
as for the unreachable nodes as strings and checked nodes as gameobjects.. I really just need to rewrite some of the script cos I overcomplicated the way I get things lol.
storing the nodes in a 2d array makes sense, but I regenerate the grid whenever I check to find the path, and I'm not entirely sure how well I could go about that.. but I'll probably wind up doing that cos it sounds a lot smarter lol.

#

very confused as to why it acts this way then

olive tartan
#

How are you getting the Tileo ?

#

Also, if you didn't want to use 2D array and care about it's size and out of bounds stuff, then Dictionary<Vector2,GridBehave> could work aswell

lean onyx
#

sorry I should've posted back here when I made some progress.
I actually have muddled around and got it basically working now

#

just one hiccup

#

but 1 sec tileo..

olive tartan
#

oh nice

lean onyx
#

Tileo is the found grid tile from the function for the pathfinding. it just finds it based on an x and y number

#

basically it's the tile in the middle that the function searches outwards from

#

@olive tartan one issue I'm having rn is I get the entire path, which I can tell by prints to the console and changing the color of the tile, but for some reason the first 2 tiles no matter what, seem to not get added to the path list

#

to add to the list, I do this
if (CheckTileobj.gameObject.GetComponent<GridBehave> ().weight < relw2) {

#

within there is the print, and color change I mentioned

#

also in there, is GOTPATH.Add (CheckTileobj.gameObject);

#

the print and color change happens, and every other tile but the last two tiles get added to the path

#

every other tile gets added as expected

olive tartan
#

are you getting any errors?

lean onyx
#

nope.

#

and if I let it play out, the correct path all gets highlighted in green

#

but only the last two green tiles aren't added to the path list

olive tartan
#

That is very strange.

lean onyx
#

yeah not really understanding how this happens lol

olive tartan
#

you could try printing something after the GOTPATH.Add , or even trying to print the CheckTileobj.gameObject to see if it isn't null or something.

#

I have no idea what could be causing it and "debugging" it out of context is a bit hard ๐Ÿ˜„

lean onyx
#

yeah. I wish I had more info rn cus I have no idea what is causing this behavior. it seems impossible to me lol

#

I'll try doing some debug.logs in places and see if I can find somethin

#

inside there I call

                            print (CheckTileobj.gameObject.name);```
and I get the current tile printed to the output, but gotpath remains with nothing in it.
repeats 2 times, and then the rest of the tiles get added correctly.
#

boutta try to make a workaround lol.

olive tartan
#

are you clearing or reinitializing the GOTPATH anywhere?

lean onyx
#

oml.

#

yes.

#

I thought that wouldn't happen until after that happened

#

I think this will fix it

#

it works perfectly now

#

thank you so much

olive tartan
#

no problem ๐Ÿ™‚

lean onyx
#

should I archive the thread now ya think?

#

I've never done threads on discord b4

olive tartan
#

if it works I don't see why not ๐Ÿ˜„

lean onyx
#

lol

#

word