#Why is my Coyote Timing not working

1 messages · Page 1 of 1 (latest)

shell lion
#

I'm trying to implement coyote timing to the brackeys game from some dudes tutorial

#

and one line is giving me an error

#

but I have it exact the same as he does

#
extends CharacterBody2D


const SPEED = 130.0
const JUMP_VELOCITY = -300.0

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
@onready var animated_sprite = $AnimatedSprite2D
@onready var coyote_timer = $CoyoteTimer


func _physics_process(delta):
    # Add the gravity.
    if not is_on_floor():
        velocity.y += gravity * delta

    # Handle jump.
    if Input.is_action_just_pressed("jump") and (is_on_floor() || !coyote_timer.is_stopped()):
        velocity.y = JUMP_VELOCITY

    # Get the input direction: 1, 0, -1
    var direction = Input.get_axis("move_left", "move_right")
    var was_on_floor = is_on_floor()
    
    velocity = move_and_slide(velocity, Vector2.UP)
    
    if was_on_floor && !is_on_floor():
        coyote_timer.start()
    
    # Flip sprite
    if direction > 0:
        animated_sprite.flip_h = false
    if direction < 0:
        animated_sprite.flip_h = true
    
    # Play animations
    if is_on_floor():
        if direction == 0:
            animated_sprite.play("idle")
        else:
            animated_sprite.play("run")
        
    else:
        animated_sprite.play("jump")
    
    # Apply movement
    if direction:
        velocity.x = direction * SPEED
    else:
        velocity.x = move_toward(velocity.x, 0, SPEED)

    move_and_slide()
#

here is my player code

royal marsh
#

not sure why CoyoteTimer needs a double-negative function instead of a variable (!coyote_timer.is_stopped() instead of coyote_timer.is_active)?

What does CoyoteTimer look like (how did you make it)?

shell lion
royal marsh
#

is move_and_slide() a player script function? It's called twice; one after was_at_floor is instantiated and one at the end of the _physics_process()

shell lion
#

okay

#

so do I remove it

royal marsh
#

1 sec, just trying it out

shell lion
#

alr

royal marsh
#

I got it to work by using this function:

#
    # gravity
    if not is_on_floor():
        velocity.y += gravity * delta
    
    # jump
    if Input.is_action_just_pressed("jump") and (is_on_floor() || !coyote_timer.is_stopped()):
        velocity.y = JUMP_VELOCITY
        coyote_timer.stop()
    
    # input direction [-1,0,1]
    var direction = Input.get_axis("move_left", "move_right")
    
    # flip sprite
    if direction > 0:
        animated_sprite.flip_h = false;
    elif direction < 0:
        animated_sprite.flip_h = true;
    
    # play animations
    if is_on_floor():
        if direction == 0:
            animated_sprite.play("idle")
        else:
            animated_sprite.play("run")
    else:
        animated_sprite.play("jump")
    
    # run
    if direction:
        velocity.x = direction * SPEED
    else:
        velocity.x = move_toward(velocity.x, 0, SPEED)
    
    # move and slide
    var was_on_floor = is_on_floor()
    move_and_slide()
    
    # coyote timer
    if was_on_floor && !is_on_floor():
        coyote_timer.start()
shell lion
#

nice

royal marsh
#

so move_and_slide() changes the positions, then you check if the player's hovering to start the timer

#

if you want to stop your guy falling on the timer you can check if the coyote timer's still going at the gravity section

shell lion
royal marsh
#

oh that's just to stop you being able to jump however many times you can in the timer period, so say, spam spacebar 10 times to jump really high

#
jump
var was_on_floor = is_on_floor()
if Input.is_action_just_pressed("jump") and (is_on_floor() || !coyote_timer.is_stopped()):
    velocity.y = JUMP_VELOCITY
    coyote_timer.stop()
    was_on_floor = false

turns out you need to reposition was_on_floor to do that, oops

shell lion
#

thank you

harsh juniper
# shell lion

When you watch tutorials, make sure they are for godot 4