How To Coyote Jump

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

So yeah, I need help implementing Coyote Jumping but tutorials on YouTube ain’t helping at all. :(((
I’m a beginner so my code is messy :((
Can anyone tell me the code and where to put it??? (Nodes or codes )

extends KinematicBody2D

const UP = Vector2(0, -1)
const GRAVITY = 20
const MAX_SPEED = 100
const JUMP = -300
const ACCELERATION = 50

onready var animationPlayer = $AnimationPlayer

var motion = Vector2()

var is_dead = false

func _physics_process(_delta):
motion.y += GRAVITY

if is_dead == false:
	if Input.is_action_pressed("ui_right"):
		motion.x = min(motion.x+ACCELERATION, MAX_SPEED)
		animationPlayer.play("Walk")

	elif Input.is_action_pressed("ui_left"):
		motion.x = max(motion.x-ACCELERATION, -MAX_SPEED)
		animationPlayer.play("WalkLeft")

	if Input.is_action_just_pressed("ui_quit"):
		get_tree().quit()
	if Input.is_action_just_released("ui_right"):
		motion.x = 0
		animationPlayer.play("IdleRight")

	elif Input.is_action_just_released("ui_left"):
		motion.x = 0
		animationPlayer.play("IdleLeft")

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

	motion = move_and_slide(motion, UP)
	pass

	if get_slide_count() > 0:
		for i in range(get_slide_count()):
			if "Enemy" in get_slide_collision(i).collider.name:
				dead()

func dead():
is_dead = true
motion = Vector2(0, 0)
$CollisionShape2D.disabled = true
$Timer.start()

func _on_Timer_timeout():
get_tree().change_scene(“res://Scene/Levels/World1.tscn”)

if Input.is_action_just_pressed("ui_quit"):
	get_tree().quit()
:bust_in_silhouette: Reply From: njamster

tutorials on YouTube ain’t helping at all

I just searched on YouTube for “godot coyote time”, the first hit was this video, the second was this. Both videos explain it pretty well, so: not true.

Anyways, here you go:

extends KinematicBody2D

const ACCELERATION = 50
const MAX_SPEED = 100
const GRAVITY = 20
const JUMP = -300    

var motion = Vector2()
var is_jumping = false
var coyote_time    

func _ready():
	coyote_time = Timer.new()
	coyote_time.one_shot = true
	coyote_time.wait_time = 2.0
	add_child(coyote_time)
    
func _physics_process(_delta):
	if Input.is_action_pressed("ui_right"):
		motion.x = min(motion.x+ACCELERATION,  MAX_SPEED)
	elif Input.is_action_pressed("ui_left"):
		motion.x = max(motion.x-ACCELERATION, -MAX_SPEED)
	else:
		motion.x = 0

	if coyote_time.is_stopped():
		motion.y += GRAVITY

	if is_on_floor():
		is_jumping = false
		if Input.is_action_just_pressed("ui_up"):
			is_jumping = true
			motion.y = JUMP

	var was_on_floor = is_on_floor()
	motion = move_and_slide(motion, Vector2.UP)
	if not is_on_floor() and was_on_floor and not is_jumping:
		coyote_time.start()
		motion.y = 0

The first video, you get an error if you use yield. The second video just doesn’t work with some people’s code (including mine).

rahsut | 2020-11-01 13:35