# How do I make a player "slide" to a direction when he is crouching?

Attention Topic was automatically imported from the old Question2Answer platform.

Hi, I am trying to create a platformer game whereas the player can perform a slide when the user is pressing “down” and the “z” button.

As for my code, I have a `var Vector2 motion` that stores x and y movements. Then this is my code that handles that input.

`````` if is_on_floor():
if Input.is_action_pressed("ui_down") and Input.is_action_just_pressed("attack"):
motion.x += 1000

motion = move_and_slide(motion, floor_normal)
``````

The problem is when I do the corresponding input, the player just “snaps” forward for a certain distance. I tried the `lerp` function to the motion.x but it didn’t work as I expected it.

Can you help me solve the logic behind this? I’ve been trying to search the web for it for some time now. Thank you!

You would have to think of some kind of mechanics to do this. One way would be to separately store current position and destination position. Then You set the destination do +1000 and in `_physics_process` or `_process` you do something like

``````position.x = lerp(position.x,destination.x,delta)
``````

or similar. Or You could have a slide_speed variable to have a linear speed for slide action, then it would be something like

``````if position.x < destination.x:
position.x += slide_speed*delta
elif position.x > destination.x:
position.x -= slide_speed*delta
``````

This is just example code and You would have to add some things to make it work more smoothly.

I’m trying to do something similar where the character does a baseball slide while moving in a direction. This is what I have but this is incorrect. I’m really stumped. @kozaluss, you think you can provide an example with numbers in it? I’m still very newb and it helps if I see what the code looks like if it were being used.

if is_on_floor():
if Input.is_action_pressed(“ui_right”) && Input.is_action_pressed(“ui_dash”):
\$Sprite.set_rotation_degrees(-90)
\$CollisionShape2D.set_rotation_degrees(-90)
friction = false
motion.x = DASH
\$Sprite.flip_h = false
\$Sprite.play(“Dash”)
elif Input.is_action_just_released(“ui_dash”):
\$Sprite.set_rotation_degrees(0)
\$CollisionShape2D.set_rotation_degrees(0)
friction = true

Forthbasic | 2018-09-04 01:05

@Forthbasic It is hard to say from the code what should this be all about… I do not know what does motion variable stands for. But consider this: You should not think about Your game objects in “instant” terms. Instead think of them as being in some state. Lets say You have a variable state which can be “idle”,“walk”,“dash”. Then You think about how input or other events change the state of the object, for example input=uidash should change the state to dash. Then you should program the behaviours according to the current state for example with if/elif/else statement (in process or physicsprocess). if state==“dash”… and so on. Then It gets more complicated, because let’s say “walk” should last as long as input is being pressed and turn into “idle” as soon, as input released. But “dash” should probably be triggered if input received and then should have some kind of timer, 1 second for example, while character moves smoothly from place to place and plays dash animation. Then when dash time ends state should return to “idle”. Think in such terms about Your objects - as they are always in some state and there are transitions from state to state.

As for the actual code - it really depends on all other factors and mechanics of the game. You can accomplish movement by changing the position of character manually in physicsprocess or in process functions. But You can also use Tween node to do it for You - You just set it up, fire and forget. Also it is important if it is a physics object of RigidBody type then You should rather apply forces to it instead of moving it manually. So - there are so many possibilities, that it is hard to shoot an example, which will be accurate for Your unique situation.

kozaluss | 2018-09-04 08:28

Thank you very much for your input! I will take your advice and change my method of thought. I have a lot to learn about godot physics.

Forthbasic | 2018-09-04 18:55