The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

sorry i know it getting annoying but im always changing how physics work in this fangame
and yeah how do i add jump animation!

extends KinematicBody2D


export var input_left : String = "left"
export var input_right : String = "right"
export var input_jump : String = "jump"


export var max_jump_height = 150 setget set_max_jump_height

export var min_jump_height = 40 setget set_min_jump_height

export var double_jump_height = 100 setget set_double_jump_height

export var jump_duration = 0.3 setget set_jump_duration

export var falling_gravity_multiplier = 1.5

export var max_jump_amount = 1
export var max_acceleration = 3500
export var friction = 8
export var can_hold_jump : bool = false

export var coyote_time : float = 0.1


export var jump_buffer : float = 0.1



var max_speed = 300
var acceleration_time = 10



var default_gravity : float
var jump_velocity : float
var double_jump_velocity : float
var release_gravity_multiplier : float


var jumps_left : int
var holding_jump := false

var vel = Vector2()
var acc = Vector2()

onready var coyote_timer = Timer.new()
onready var jump_buffer_timer = Timer.new()


func _init():
    default_gravity = calculate_gravity(max_jump_height, jump_duration)
    jump_velocity = calculate_jump_velocity(max_jump_height, jump_duration)
    double_jump_velocity = calculate_jump_velocity2(double_jump_height, default_gravity)
    release_gravity_multiplier = calculate_release_gravity_multiplier(
        jump_velocity, min_jump_height, default_gravity)


func _ready():
    add_child(coyote_timer)
    coyote_timer.wait_time = coyote_time
    coyote_timer.one_shot = true

    add_child(jump_buffer_timer)
    jump_buffer_timer.wait_time = jump_buffer
    jump_buffer_timer.one_shot = true


func _physics_process(delta):
    acc.x = 0



    if is_on_floor():
        coyote_timer.start()
    if not coyote_timer.is_stopped():
        jumps_left = max_jump_amount

    if Input.is_action_pressed(input_left):
        acc.x = -max_acceleration
        $Sprite.flip_h = true
        $AnimationPlayer.play("walk")


    elif Input.is_action_pressed(input_right):
        acc.x = max_acceleration
        $Sprite.flip_h = false
        $AnimationPlayer.play("walk")
    else:
       acc.x = 0
       if is_on_floor():
           $AnimationPlayer.play("idle")



    if can_hold_jump:
        if Input.is_action_pressed(input_jump):
            if is_on_floor():
                jump()

    if not can_hold_jump:
        if not jump_buffer_timer.is_stopped() and is_on_floor():
            jump()

    if Input.is_action_just_pressed(input_jump):
        holding_jump = true
        jump_buffer_timer.start()

        if not is_on_floor():
            jump()





    if Input.is_action_just_released(input_jump):
        holding_jump = false


    var gravity = default_gravity

    if vel.y > 0: 
        gravity *= falling_gravity_multiplier

    if not holding_jump and vel.y < 0: 
        if not jumps_left < max_jump_amount - 1: 
            gravity *= release_gravity_multiplier 

    acc.y = -gravity
    vel.x *= 1 / (1 + (delta * friction))

    vel += acc * delta
    vel = move_and_slide(vel, Vector2.UP)



func calculate_gravity(p_max_jump_height, p_jump_duration):
    return (-2 *p_max_jump_height) / pow(p_jump_duration, 2)


func calculate_jump_velocity(p_max_jump_height, p_jump_duration):
    return (2 * p_max_jump_height) / (p_jump_duration)


func calculate_jump_velocity2(p_max_jump_height, p_gravity):
    return sqrt(-2 * p_gravity * p_max_jump_height)


func calculate_release_gravity_multiplier(p_jump_velocity, p_min_jump_height, p_gravity):
    var release_gravity = 0 - pow(p_jump_velocity, 2) / (2 * p_min_jump_height)
    return release_gravity / p_gravity


func calculate_friction(time_to_max):
    return 1 - (2.30259 / time_to_max)


func calculate_speed(p_max_speed, p_friction):
    return (p_max_speed / p_friction) - p_max_speed


func jump():
    if jumps_left == max_jump_amount and coyote_timer.is_stopped():
        jumps_left -= 1

    if jumps_left > 0:
        if jumps_left < max_jump_amount: 
            vel.y = -double_jump_velocity
        else:
            vel.y = -jump_velocity
        jumps_left -= 1


    coyote_timer.stop()


func set_max_jump_height(value):
    max_jump_height = value

    default_gravity = calculate_gravity(max_jump_height, jump_duration)
    jump_velocity = calculate_jump_velocity(max_jump_height, jump_duration)
    double_jump_velocity = calculate_jump_velocity2(double_jump_height, default_gravity)
    release_gravity_multiplier = calculate_release_gravity_multiplier(
        jump_velocity, min_jump_height, default_gravity)


func set_jump_duration(value):
    jump_duration = value

    default_gravity = calculate_gravity(max_jump_height, jump_duration)
    jump_velocity = calculate_jump_velocity(max_jump_height, jump_duration)
    double_jump_velocity = calculate_jump_velocity2(double_jump_height, default_gravity)
    release_gravity_multiplier = calculate_release_gravity_multiplier(
        jump_velocity, min_jump_height, default_gravity)


func set_min_jump_height(value):
    min_jump_height = value
    release_gravity_multiplier = calculate_release_gravity_multiplier(
        jump_velocity, min_jump_height, default_gravity)


func set_double_jump_height(value):
    double_jump_height = value
    double_jump_velocity = calculate_jump_velocity2(double_jump_height, default_gravity)
Godot version im using version 3.5
in Engine by (29 points)
edited by

also the code is the platformer body 2d plugin

I'd recommend editing your question, highlighting all the code, and then hit the code sample button, should look elike { }. That way the the entire thing becomes plain text format. Makes it easier to read.

thanks i didnt know how it worked!

1 Answer

0 votes
Best answer

Should be simple enough. Assuming your animation is named "jump" and you have an AnimatedSprite2D, it's as easy as:

if Input.is_action_just_pressed('jump'):
       $AnimatedSprite2D.play('jump')

You just have to make sure to program logic for the jump to END as well. And for that double jump you probably want a variable to track how many jumps you've done and then reset it once you hit the ground.

by (562 points)
selected by

thanks i had to make some changes to fit the code but it worked none the less

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 Frequently asked questions and 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 [email protected] with your username.