0 votes

I'm having an issue in trying to implement double jump into my game. The issue I'm having is that when the player character jumps into a wall, the player character glides up the wall instead of just jumping off the wall. Why is this happening, and how can this be fixed?

This is my code for the player character:

extends KinematicBody2D

const jumpforce = -600
const maxspeed = 2048
var acceleration = 4
var gravity = 0
var velocity = Vector2(0,0)

onready var LeftRayCast = $LeftRayCast
onready var RightRayCast = $RightRayCast

# This function runs the loop that keeps the character running
func _physics_process(delta):
    run()
    jump()
    friction()
    gravity()

    #Moves the ball
    velocity = move_and_slide(velocity, Vector2.UP)

func run():
    if Input.is_action_pressed("right") and not is_on_wall():# Move right
        if velocity.x < 0:
            velocity.x += acceleration * 2
        else:
            velocity.x += acceleration
    if Input.is_action_pressed("left") and not is_on_wall():# Move left
        if velocity.x > 0:
            velocity.x -= acceleration * 2
        else:
            velocity.x -= acceleration

func jump():
    if Input.is_action_pressed("up"):# Jump
        if is_on_floor():# Normal Jump
            velocity.y += jumpforce
        elif not is_on_floor() and on_left_wall():# Left wall jump
            velocity.y = jumpforce
            velocity.x = jumpforce/2
        elif not is_on_floor() and on_right_wall():# Right wall jump
            velocity.y = jumpforce
            velocity.x = -jumpforce/2
        #elif is_on_wall():# Wall Jump
        #   if velocity.x > 0 and Input.is_action_pressed("right") and not Input.is_action_pressed("left"):
        #       velocity.y = jumpforce
        #       velocity.x = jumpforce/2
        #   if velocity.x < 0 and Input.is_action_pressed("left") and not Input.is_action_pressed("right"):
        #       velocity.y = jumpforce
        #       velocity.x = -jumpforce/2

func on_left_wall():
    return $LeftRayCast.is_colliding() and Input.is_action_pressed("left")

func on_right_wall():
    return $RightRayCast.is_colliding() and Input.is_action_pressed("right")

func gravity():
    if is_on_floor():
        if Input.is_action_pressed("down"):# Slide down a slope
            gravity = 30
        else:
            gravity = 5
    else:
        gravity = 20
    velocity.y = velocity.y + gravity
Godot version 3.4
in Engine by (12 points)

When it comes to the wall

Print(gravity) and see the result.

1 Answer

0 votes

try this for jump function instead:

func jump():
if Input.is_action_just_pressed("up"):# Jump
    if is_on_floor():# Normal Jump
        velocity.y += jumpforce
    elif not is_on_floor() and on_left_wall():# Left wall jump
        velocity.y = jumpforce
        velocity.x = jumpforce/2
    elif not is_on_floor() and on_right_wall():# Right wall jump
        velocity.y = jumpforce
        velocity.x = -jumpforce/2
if Input.is_action_just_released("up"):
    if not is_on_floor() and on_left_wall() or on_right_wall():
       velocity.y = 0
by (307 points)
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.