I’m trying to make a loop where an object (Heart) travels in a path by going to the nearest chosen object (Fetch). The idea I had was that Heart goes to the nearest Fetch object, Fetch gets deleted, so Heart will go to the next Fetch object, and so on.
Problem is that, it will go to the first Fetch object and then just keep going in that one direction. It won’t go to the next one. I’m sure that’s because I put a Permanent Force for the action, but I’m not sure what else to do, instead.
I’ve tried to use the Pathfinding extension, but I didn’t like it.
You’re applying a force toward a Fetch object you’ve just deleted. So I guess GDevelop is going “yeah, nah” and just ignoring the action.
You haven’t told GDevelop which Fetch object to move to, and you’ve made the same mistake in the beginning of scene sub event. You’ll need to pick all instances of Fetch, then pick the closest one and then move Heart towards it.
When using a permanent force, make sure you stop the object first. Otherwise, depending on the directions, it will either speed up the object or cancel some of the force.
Collision between heart
... Trigger once
------------------
... Delete Heart
-------------------
... Pick all Heart objects
... Pick nearest Heart
... Stop Fetch
... Apply force towards Heart
A tween would also work.
At the beginning
Pick the nearest Heart
.... Tween to heart.X() and Y()
Tween is finished
... Delete Heart
----------------
... Pick all Heart objects
... Pick the nearest Heart
.... Tween to heart.X() and Y()
I’m going on the assumption Keith_1357 is on mobile, or it was really late for him. Here’s a corrected version of his response:
Collision between Heart and Fetch
Trigger once
------------------
.... Delete Fetch
.... Pick all Fetch objects
.... Pick nearest Fetch
.... Stop Heart
.... Apply force towards Fetch
And the tween:
At the beginning
Pick the Fetch nearest to Heart
------------------
.... Tween Heart to Fetch .X() and Y()
Heart Tween is finished
Collision between Heart and Fetch
------------------
.... Delete Fetch
.... Pick all Fetch objects
.... Pick the Fetch nearest to Heart
.... Tween Heart to Fetch .X() and Y()
Here’s the code with the tweens. It works a little better. I’m gonna play with it a bit more tomorrow and see if I can make the speed more consistent, since Heart will go slow one minute and then fast the next because some of the Fetch objects are closer or farther apart due to the level’s sharp turns.
When the player starts the game, Heart will find the nearest Fetch object and then fly to it. After it gets one, it goes to the next and will stop when it’s at the door. One thing I had to make sure to do was to lay the Fetch objects out close enough with turns so that Heart stays on the path I want it to go in.