How to make the player slide on ice after finising run or walk

Hello folks, I am quite new to defold and game development in general.

I am working on my first game on defold. I am using the platformer template as a base.

In my game, platforms have ice and are slippery.

I would like some guidance on how to make the player slide a little after finishing the run or walk. I figured out linear interpolation might be useful for this use case, but have no idea how to use it on the code.

My player script is more or less same as this https://github.com/defold/template-platformer/blob/main/game/player.script

I would use the following:

@britzl I meant like little slide after releasing keys, I guess mega man ice level had this kind of thing.

I wanted something like this, The player walks normally when I press Left/Right, and when I release the key, instead of stopping at once, the player will slide a little then stop, something like slipping. The velocity will keep pushing the player a little after stopping the walk.

Ah, I didn’t read your question properly. Ok, then I’d change my walk function to track if the player is walking or not:

self.walking = (direction ~= 0)

I would also set the horizontal velocity based on self.facing_direction instead of direction (which may be 0):

self.velocity.x = max_speed * self.facing_direction

And in your fixed_update() I’d look at self.walking and if the player is no longer walking I’d apply some damping to self.velocity.x so that it decreases over time.

1 Like

something like this?

        local slide_factor = 0.9
	local pos = go.get_position()

	if not self.walking then
		self.velocity.x = self.velocity.x * slide_factor
	end

	pos = pos + self.velocity * dt

	go.set_position(pos)

Yes, but you probably also want to factor in dt when you applying the damping on the velocity.

1 Like

Hey, that’s the right direction!
I took a look on the platformer template. Personally, I would probably change how the movement works by integrating acceleration and friction. Currently, the velocity is calculated with a fixed speed and direction. And by adding acceleration and friction to your movement system, you can use something like vmath.lerp(friction, velocity.x, 0) to achieve a smooth stop. Changing to a lower friction value would cause your character to do a long slide before stopping completely.

1 Like

Another thing I could not understand was where is this 0.7 coming from (https://github.com/defold/template-platformer/blob/1a025938c81fec823f28bba2d776b5dc499fa525/game/player.script#L119)

if normal.y > 0.7 then

Since you are using a normalized normal you can use the fact that the normal is always on the unit circle.
So, using the normal.y will give you the angle of the slope (in radians).
0.7 in radians is ~40 degrees.
A value of 1.0 would mean that the normal is pointing straight up (i.e. 90 degrees)

So, if the slope is < ~40 degrees, then you have ground contact.

I experimented with few methodology, but I found that vmath.lerp is not actually giving a zero value finally, the value gets smaller and smaller but not zero, thus the animations is never going to idle, how do I circumvent this issue?