All Questions

Community

N
Kgothatso Ntuli

After implementing coyote jump unable to double jump.

Could you take a look to see why my character won't double jump everything else still works.

Air.gd

extends State

signal jump_pressed

onready var coyote_jump:  Timer = $CoyoteJump

export var acceleration_x := 5000.0
export var jump_force := 750.0
export var max_jump_count := 2

var jump_count = 0


func unhandled_input(event: InputEvent) -> void:
	var move := get_parent()
	if event.is_action("jump"):
		emit_signal("jump_pressed")
		if move.vel.y >= 0.0 and coyote_jump.time_left > 0.0 and jump_count < max_jump_count:
			jump()
	else:
			move.unhandled_input(event)


func physics_process(delta: float) -> void:
	var move := get_parent()
	move.physics_process(delta)

	# Landing
	if owner.is_on_floor():
		var target_state := "Move/Idle" if move.get_move_dir().x == 0 else "Move/Run"
		_state_machine.transition_to(target_state)


func enter(msg: Dictionary = {}) -> void:
	var move := get_parent()
	move.enter(msg)
	
	move.acceleration.x = acceleration_x
	if "vel" in msg:
		move.vel = msg.vel 
		move.max_speed.x = max(abs(msg.vel.x), move.max_speed.x)
	if "force" in msg:
		jump()
#	else:
#		jump_count += 1
	
	coyote_jump.start()

func exit() -> void:
	var move := get_parent()
	move.acceleration = move.acceleration_default
	jump_count = 0
	move.exit()

func jump() -> void:
	var move := get_parent()
	move.vel += calculate_jump_vel(jump_force)
	jump_count += 1
"""
Returns a new velocity with a vertical force applied to it
"""
func calculate_jump_vel(force: float = 0.0) -> Vector2:
	var move: State = get_parent()
	return move.calculate_vel(
		move.vel,
		move.max_speed,
		Vector2(0.0, force),
		Vector2.ZERO,
		1.0,
		Vector2.UP
	)

Move.gd

extends State


export var max_speed_default := Vector2(450.0, 1500.0)
export var acceleration_default := Vector2(1000000, 3000.0)
export var decceleration_default := Vector2(3000, 3000.0)
export var max_speed_fall := 2500.0


var acceleration := acceleration_default
var decceleration := decceleration_default
var max_speed := max_speed_default
var vel := Vector2.ZERO


func unhandled_input(event: InputEvent) -> void:
	if owner.is_on_floor() and event.is_action_pressed("jump"):
		_state_machine.transition_to("Move/Air", { force = true })


func physics_process(delta: float) -> void:
	vel = calculate_vel(vel, max_speed, acceleration, decceleration, delta, \
	get_move_dir())
	vel = owner.move_and_slide(vel, owner.FLOOR_NORMAL)
	
func enter(msg: Dictionary = {}) -> void:
	$Air.connect("jump_pressed", $Idle.jump_pressed_delay, "start")
	
func exit() -> void:
	$Air.disconnect("jump_pressed", $Idle.jump_pressed_delay, "start")

static func calculate_vel(
		old_vel: Vector2,
		max_speed: Vector2,
		acceleration: Vector2,
		decceleration: Vector2,
		delta: float,
		move_dir: Vector2,
		max_speed_fall := 2500.0
	) -> Vector2:
	var new_vel := old_vel

	new_vel.y += move_dir.y * acceleration.y * delta
	if move_dir.x:
		new_vel.x += move_dir.x * acceleration.x * delta
	elif abs(new_vel.x) > 0.1:
		new_vel.x -= decceleration.x * delta * sign(new_vel.x)
		new_vel.x = new_vel.x if sign(new_vel.x) == sign(old_vel.x) \
		else 0
		
	
	new_vel.x = clamp(new_vel.x, -max_speed.x, max_speed.x)
	new_vel.y = clamp(new_vel.y, -max_speed.y, max_speed_fall)

	return new_vel


static func get_move_dir() -> Vector2:
	return Vector2(
		Input.get_action_strength("move_right") - Input.get_action_strength("move_left"),
		1.0
	)


  • Nathan Lovato replied

    It's always a bit tricky to debug a lot of code like that just from reading it, but it might be that the line

    if event.is_action("jump"):
    

    Needs to be

    if event.is_action_pressed("jump"):
    

    Otherwise, it also triggers when you release the key.

    I would say you probably want to stop the timer node upon exiting the air state.

    Could you try and tell me if that helps?

  • N
    Kgothatso Ntuli replied

    This has worked however, the jump is inconsistent. Sometime the player jumps really high other times it's barely noticeable.