# Move a fixed distance

I am fairly new to GDevelop and I’ve been able to figure things out on my own, until now.

I am making a Frogger style game and this is what I’m trying to achieve:

When a direction key is pressed, move the player exactly 100 pixels in that direction. But I want the player to actually move and not simply jump/teleport 100 pixels away.

I’ve tried several different methods but nothing seems to work. I tried using a force to move the player and then stopping the forces after the player has traveled 100 pixels. That almost works but the player always goes a little too far.

The map is designed on a grid of 100x100 pixel blocks. That’s why the movement needs to be exactly 100 pixels every time.

This type of movement seems like it should be easy to achieve but I haven’t had any luck…

Like a classic RPG?, I would do it this way:
RPGMovement.zip (5.37 KB)
It’s very condensed, the importat thing is to calculate a destination when you press a key (for example if you press the Right key, the destination will be current X position + 100), move until you reach the destination, then stop the player and set the position = destination, this way the player will be always inside the grid, without exceeding it.

I was actually using a similar method but mine wasn’t nearly as complex (or versatile) as that example. My biggest mistake was using forces for movement because I was using a timer to limit how fast a key could be pressed. Using the “moving” variable as a switch to control the movement, is a much better solution.

Thank you!

Updated, added support for GD5 and changed the code and comments a bit:
RPGMovement.zip (8.77 KB)

3 Likes

AMAZING! Thanks so much!

But how do you deal with collisions?

I would normally use “separate two objects” but the player freezes when it comes into contact with a collision.

You have to move every object based on grid positions (check in wich virtual cell the object will be placed based in the direction you’re trying to move it… you can manage it from memory with a variable structure) and based on those values set your own collision system to prevent colliding objects to be placed in the same virtual cell. That’s RPG grid-based movement and collision.

Or set a condition to cancel the movement towards a direction when the object hits a collision. I mean, cancel all the conditions that performs the movement, not just block it, because otherwise the object will freeze in a collision.

Hi guys, I was wondering if we can use this code with the Pathfinding behavior, because I tried but it doesn’t seems to work properly, even if I try to simulate the controls…
Will I have to recode the entire pathfinding behavior ? ;-;

Is this the same extension called PixelPerfectMovement? Because I’ve imported it but I cannot find it.

Hi @AntonioNero, please don’t reactivate discussions that are many years old, just open a new topic in the forum. In 2018 an older version of Gdevelop, GD 4, was used which is very different from GD 5. Example projects from this time are not compatible with current versions of Gdevelop.

1 Like