Move object from A to B position and then return back


I need your help please. I need to move an object from A to B position (Y position) and then, after 2 second return back to the A initial position.

Someone can help me please? Thanks!

If it is just a simple vertical movement then two events should do it.

Event 1 starts the movement and a timer. Event checks when the timer is at 2 seconds and then moves it back.
For the sake of the example I’ll assume that the trigger for the object called “thing” moving is the player character colliding with it.

Event 1 :

Player is in collision with thing       |   Reset the timer  thingmove
                                           Add to thing a force of 0p/s on X axis and 100 p/s on Y axis

Event 2:

The timer "thingmove" is greater than 2 seconds   |   Add to thing a force of 0p/s on X axis and -100 p/s on Y axis

If you have more than one “thing” object then you will have to give them each an object variable and check this in event 2’s condition, to stop them all moving.

Hi! I just finished making an example but MattLB beat me to the punch. :stuck_out_tongue:

But here’s an example just to see it in action. It’s the same as Matt’s explanation but in this example the object starts moving from point A to point B after 2 seconds at the start of the scene and stops when it has reached point B for 2 sceonds then goes back to point A again and waits for another 2 seconds before repeating the process in a forever loop. You can remove the code that you don’t need if you don’t want it to loop forever.

I’m guessing it’s for an enemy AI movement? :slight_smile:

Edit: This version doesn’t include collisions, just Y coordinates.
Point A to Point B.rar (10.7 KB)


thanks to both of you! so basically in the examples provided by you the added force must be stopped by another event (collision or checking the Y cordinates) right?
Now I’m trying to use Pathfinding Behavior, it seams more simple if I understand wel…

abelazo’s sensibly put in some checks that I didn’t. :wink:

In my example the force is only added when the collision is happening, so the force will stop being added quite quickly and so the object will stop on its own. The return journey would have a stopping problem unless you reset the thingmove timer in event 2, but then you might have to play around with different forces until it stopped in the right place.

Overall it is probably better to store the starting y position (A) in an object variable and the destination y position (B) in another variable. These can be set up at the start of the scene. I’ve changed to “square” for the object to match up with abelazo’s example file.

At the beginning  of the scene |   Do = square.Y() to variable positionA of square
                                 Do = square.Y()+200 to variable positionB of square

This means the “square” object will always move 200 pixels up and down from wherever it is placed in the scene, so if you move it in the editor you don’t have to change the numbers, they will update themselves. Obviously, you change it to -200 if you want it to move the other way.

You can then check if the Y position of square equals square.Variable(positionB) or square.Variable(positionA).

Another possibility would be using the “Add a force to move towards a position” action, since you now have those positions stored as variables.