How to make a object move toward another specific object in a group

Ok let’s say I have four bear cubs and four beds, one for each cub.

Each bed and bear cub has an ID from 1-4. Cub ID 1 needs to move to bed ID 1, cub 2 goes to bed 2, cub 3 to bed 3 and so forth.

How can this be achieved in Gdevelop? I have tried several ways but could not pick the right bed, all four cubs would either go to one bed or not move at all.

Can you post a screenshot of what you tried? Are there obstacles? You can use force, Tweening or the Pathfinder behavior. The pathfinder behavior would be easier and more natural looking. It depends on if you have obstacles and how you want them to move.

I am using pathfinder behavior. the moving method doesnt matter, I know how to do that, the main problem is getting the right cub to the right bed.

Trigger the following once.

For each Bear
… ID of Bed equals ID of Bear
… Move Bear to Bed.X(), Bed.Y()

I tried that and it didnt work before


For each bed:
If the bear’s “id” variable equals bed.id
Move the bear to bed.X bed.Y
GIF 06.10.2026 13-59-16

Check the IDs. Make sure each object is numbered 1-4.


i cant test it, but will this code work?

Thanks, Kotiks, will try this one.

Solved, thanks kotiks your solution worked!

cubs

I would split the actions. Unless the destination changes or something else can change and block the path, you don’t need to repeatedly trigger the move action. The pathfinder move action can use a lot of resources.

It’s also best to test if a path was found. In my example, it doesn’t change the boolean unless a path was found. That way, it would keep trying to move until a path was found. That’s only needed if a path could be blocked. Otherwise, you could just immediately set the boolean to true.

I also added a 2nd boolean so it ignores bears already in bed.

For testing, I used a mouse down to start things.

Typo. I had a variable condition instead of action.

Thanks for the tips, I have incorporated your logic into the cub’s pathfinding instructions.