0 votes

I want that if the player goes +2 or -2 taking into account the x position of the player before pressing left or right, the fall animation change and keeps changed (even if turning to the other side) until the player lands.
https://imgur.com/a/964KXn6
Here's my code:

extends KinematicBody2D


const UP = Vector2(0, -1)
const GRAVITY = 20
const MAXFALLSPEED = 200
const MAXSPEED = 80
const JUMPFORCE = 300
const ACCEL = 10

var motion = Vector2()

func _physics_process(delta):

    motion.y += GRAVITY
    if motion.y > MAXFALLSPEED:
        motion.y = MAXFALLSPEED

    motion.x = clamp(motion.x, -MAXSPEED, MAXSPEED)

    if Input.is_action_pressed("ui_right"):
        motion.x += ACCEL
        $Spr_P.set_flip_h(false)
        if is_on_floor():
            $AnimationPlayer.play("Walk")
        if !is_on_floor():
            if motion.y < 0:
                $AnimationPlayer.play("Jump")
            elif motion.y > 0:
                if position.x + 1:
                    $AnimationPlayer.play("Fall Forward")
                else:
                    $AnimationPlayer.play("Fall")
    elif Input.is_action_pressed("ui_left"):
        motion.x -= ACCEL
        $Spr_P.set_flip_h(true)
        if is_on_floor():
            $AnimationPlayer.play("Walk")
        if !is_on_floor():
            if motion.y < 0:
                $AnimationPlayer.play("Jump")
            elif motion.y > 0:
                if position.x - 1:
                    $AnimationPlayer.play("Fall Forward")
                else:
                    $AnimationPlayer.play("Fall")
    else:
        motion.x = lerp(motion.x, 0, 0.2)
        if is_on_floor():
            $AnimationPlayer.play("Idle")
        if !is_on_floor():
            if motion.y < 0:
                $AnimationPlayer.play("Jump")
            elif motion.y > 0:
                $AnimationPlayer.play("Fall")

    if is_on_floor():
        if Input.is_action_just_pressed("ui_up"):
            motion.y = -JUMPFORCE

    motion = move_and_slide(motion, UP)
in Engine by (122 points)
edited by

I'm not sure if I understand properly. What do you mean by "if the player goes +2 or -2"? Are you talking about the x-position of the player? In pixels? Or tiles?

Pixels (i tried if position.x + 2/- 2 (well, i still have to see how much) but it doesn't keeps the change).

I'm assuming that you're talking about the "Fall Forward" animation in the image. There are some issues with your if statements.

position.x + 1 is an integer, not a boolean. When converting ints to bools, anything that's not 0 is true, so the "Fall" animation will only play if the player is at 1 or -1.

But what do you mean by "if the player goes +2 or -2 taking into account the x position of the player before pressing left or right" still? Are you saying you want to play a new animation and not stop it if the player character moves two pixels after pressing the left or right button? And do you want to flip the animation or not?

My animation of the middle ("Fall") it's used if the player jumps without using right or left (x + 0) or before reaching +2/-2 pixels (if the x position before jumping was 960, then when 962 or 958 is reached change the animation to "Fall Forward" (if motion.y > 0 too) and keep it (without mattering if the player releases right or left or turns to the other side) until the character lands.
And yes, the animations have to be flipped.
Edit: Solved.

1 Answer

+1 vote
Best answer

You should store the original x position of the player in a variable right before the player jumps and then check to see if the current position is at least two away from the original x position and play the "Fall Forward" animation. Also, you've got a bunch of repeated code in your function that could probably be taken out of their if statements.

by (7,541 points)
selected by

Yeah, i have to optimize my code. Thanks!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.