Need help with pathfinding in a rts game

Hello, Im trying to make a rts style game with units that can be sent out to mine rocks I have spread over the map. I want to use the path finding behavior so that they will only go to the specific rock designated while avoiding the other rocks. I started by giving the units each a object variable known as ID so they would all have there own ID number (1,2,3 etc). Then I set it so when you click on one it sets a scene variable to that object variable ID so the i can refer to it later and make sure only the selected unit moves. Then I made it so if you click on a rock only the unit with the ID matching the scene variable would receive the coordinates of the rock stored in object variables XD,YD. Than I have it so all units should always be pathfinding to cordinates XD and YD. I’ve tried tweaking my code in many ways but cant get it too work.

Any ideas on how I could make it work, or just a simpler way to do it in general?

-Thank you in advance and sorry if my explanation isn’t great

Hi!
If I’m not mistaken, then your check of the value of the ID variable of the Worker and the SelectedID value does not work correctly, to find a suitable instance of the Worker object, you need to loop over the object and then compare, in this case you will find a suitable instance.
Alternatively, when you click on the stone (Object), you check whether which unit is selected (SelectedID is greater than 0, respectively, if the selection is removed from the unit, then set to 0). If yes, then start the cycle for all units and compare the value of the variable ID and SelectedID, when found, assigns the values of the coordinates of the stone and activate the movement.

Hey thank you so much for the response , your part about the loop helped me fix the issue I was having!

1 Like

do you think you could help me with one other thing? So I made the rocks impassable objects so units would avoid other rocks to get to the selected one. The issue is apparently you cant path find to a location that’s within a impassable object, so I cant make a unit actually go to a rock that’s selected.

I haven’t used the pathfinding behavior, so I can’t say exactly yet.
Perhaps you should calculate a point near the rock. When choosing a stone, and having determined where the unit is standing relative to it, you calculate the point to which the unit must come to be close to the stone (for example, if the unit is to the left of the stone, then we take the current position of the center of the stone by X and subtract half the size of the stone , well, a few more pixels additionally). This will give you a point outside the stone, but very close to it.

Ok thank you I will try something like that, cheers :slight_smile:

1 Like