Page 1 of 1

Move a fixed distance

PostPosted: Mon 12 Mar 2018, 00:10
by Chris_P_Cream
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...

Re: Move a fixed distance

PostPosted: Mon 12 Mar 2018, 01:25
by Lizard-13
Like a classic RPG?, I would do it this way:
(5.37 KiB) Downloaded 105 times

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.

Re: Move a fixed distance

PostPosted: Tue 13 Mar 2018, 01:05
by Chris_P_Cream
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!

Re: Move a fixed distance

PostPosted: Thu 13 Dec 2018, 13:25
by Lizard-13
Updated, added support for GD5 and changed the code and comments a bit:
(8.77 KiB) Downloaded 4 times

Re: Move a fixed distance

PostPosted: Thu 13 Dec 2018, 16:39
by delonious
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.

Re: Move a fixed distance

PostPosted: Thu 13 Dec 2018, 17:10
by erdo-dp
delonious wrote: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.