About movement

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Tiamatsu

So I’m tryng to get used to Godot 3.0 and I’m remaking my old project. I’m at the point that I want to make a nice and smooth movement engine, so I’ve been messing around with the code.
And after a while, I got stuck.
This is my code atm. It makes the charachter move, but not as smooth as I’d like to.


func _physics_process(delta):

var move_left = Input.is_action_pressed("left")
var move_right = Input.is_action_pressed("right")
var jump = Input.is_action_just_pressed("jump") and jump_count < MAX_JUMP_COUNT

var friction = false

motion.y += GRAVITY

if move_left:
	motion.x = max(motion.x - ACCELERATION, -MAX_SPEED)
	$AnimatedSprite.flip_h = true
	$AnimatedSprite.play("run")
elif move_right:
	motion.x = min(motion.x + ACCELERATION, MAX_SPEED)
	$AnimatedSprite.flip_h = false
	$AnimatedSprite.play("run")
else:
	friction = true
	$AnimatedSprite.play("idle")
if jump:
	motion.y = JUMP_HEIGHT
	jump_count += 1

At this point, when the player presses left and, while moving left, presses right, it keeps going left instead of changing direction.
But if player presses right and, while moving right, presses left, it does change direction and change it back to right again once left is released.


So this is what I want to fix: I want the charachter to move into the last direction that’s been pressed, but if it’s released and the first one is still pressed, it changes back to that first direction that is still pressed.


Example of what I want to happen (since I think I didn’t explain it really well):

  • Player presses left and starts moving to the left.
  • While keeping left pressed and moving to the left, player presses right and changes direction and now moves to the right.
  • If player releases right, it moves to the left again (because left is still pressed).

And exactly the same for the inverted situation (pressing right in the first place instead of left).


I’ve been tryng to make it work, but though it’s probably easy af, I’m really stuck.

:bust_in_silhouette: Reply From: Bartosz

Try storing last actions like this:

var actions = []

func _physics_process(delta):
    if Input.is_action_just_pressed("right"):
        actions.push_back("move_right")
    if Input.is_action_just_pressed("left"):
        actions.push_back("move_left")
    if Input.is_action_just_released("left"):
        var idx = actions.find("move_left")
        actions.remove(idx)
    if Input.is_action_just_released("right"):
        var idx = actions.find("move_right")
        actions.remove(idx)
    var jump = Input.is_action_just_pressed("jump") and jump_count < MAX_JUMP_COUNT

    var friction = false

    motion.y += GRAVITY
    
    if not actions.empty():
        var latest_action = actions[actions.size() - 1]
        if latest_action == "move_left"
            motion.x = max(motion.x - ACCELERATION, -MAX_SPEED)
            $AnimatedSprite.flip_h = true
            $AnimatedSprite.play("run")
        else if latest_action == "move_right"
            motion.x = min(motion.x + ACCELERATION, MAX_SPEED)
            $AnimatedSprite.flip_h = false
            $AnimatedSprite.play("run")
    else:
        friction = true
        $AnimatedSprite.play("idle")
    if jump:
        motion.y = JUMP_HEIGHT
        jump_count += 1

Okay I think I know what you mean by storing them. I actually thought about some feature like that being in the code but I honestly did not know if it existed or how to do it.
So, am I supposed to literally copy that replacing that part of the code that I have at the moment?
Sorry if it’s a silly question, I’m just so new at this.

Tiamatsu | 2018-03-21 22:57

Copy paste should work, but it is always better to understand what code is suppose to do and write it by yourself

Bartosz | 2018-03-21 23:05

Okay, I’ll try it out tomorrow and tell you how it goes. I’ll also like give an explanation of how I undertand it (if it turns out well) to see if I got it right.
Thank you in advantage.

Tiamatsu | 2018-03-21 23:08

Great. This definitely worked. Thank you so much.


So let me know if I understood it right:

  • Im literally sotring which of the inputs are pressed with the var “actions”.
  • Then, it’s made to check which one of these is pressed last and makes it take priority over the first one.
  • So, if it is released but any other input is still pressed, it also recognizes it and makes it move into that direction.

Just one question: what do those [ ] mean for the var “actions”?

Tiamatsu | 2018-03-22 20:43

yeah, you understand it correctly.
[] means that actions are Array that starts with 0 elements

Bartosz | 2018-03-22 20:51

Oh cool, didn’t know that. Thank you again ^^

Tiamatsu | 2018-03-22 20:55