How to make a Wall jump properly ?

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

Hi !

I’m trying to make a platform game, and of course I need THE wall jump.

I tried that when the character touch a wall, he stops moving, and when I push the “ui_right” he goes right and he doesn’t stick to the wall, but it doesn’t work :confused:

Do you have an other way to do it ?

My code :

`

extends KinematicBody2D

const UP = Vector2(0,-1)
const GRAVITY = 20
const SPEED = 200
const JUMP_HEIGHT = - 550


var motion = Vector2()
var motion_2 = Vector2()

var wall = false

func _physics_process(delta):



#not is_on_wall() and wall == false

if wall == false:
	GRAVITY = 20
	motion_2.y -= GRAVITY
	motion_2.x -= GRAVITY

	motion.y += GRAVITY
	
	
	if Input.is_action_pressed("ui_right"):
		
		motion.x = SPEED
		$AnimatedSprite.play("Run")
		$AnimatedSprite.flip_h = false
		
	
	elif Input.is_action_pressed("ui_left"):
		motion.x = -SPEED
		$AnimatedSprite.play("Run")
		$AnimatedSprite.flip_h = true
		
	
	elif Input.is_action_just_pressed("Dash"):
		if $AnimatedSprite.flip_h == true:
			move_local_x(-50)
		elif $AnimatedSprite.flip_h == false:
			move_local_x(50)
		
	else :
		motion.x = 0
		$AnimatedSprite.play("Idle")
	
	if is_on_floor():
		print("floor")
		if Input.is_action_just_pressed("ui_up"):
			motion.y = JUMP_HEIGHT
			

if is_on_wall():
	print("wall")
	
	wall = true
	
	
#		motion.x = 0
#		motion.y = 0
#		GRAVITY = 0
#		$AnimatedSprite.play("Idle")

	if Input.is_action_pressed("ui_right"):
		print("right")
		wall = false`
:bust_in_silhouette: Reply From: Socrates

There are a number of of ways to implement this. In my own game I don’t use is_on_wall() at all, because this will activate the wall jump on walls where you don’t want it (e.g. it’s usually desirable for a player to be able to slide upwards along a wall to reach the top of a platform that is of a jumpable height). Instead I have an Area2d attached to the player (a raycast would would work as well). Then I place Area2ds on the walls I want to be jumpable and put them on the collision layer of the character’s Area2d’s collision mask. There is nothing else on this collision layer, just jumpable wall Area2ds. Then you can just connect the character’s Area2d to a function that stops normal movement etc.

Thanks for replying, but how do you code the fact that he stays on the wall, and when you press right + jump, or left + jump, he does the action ?

Thanks :wink:

mister_why | 2018-03-12 22:32