- Edited
Now i have asked this question (on reddit and Godot forums) before but nobody was able to solve it. I am not able to do it on my own. And you better not direct me towards Game-endeavors videos or any state-machine tutorials. Game-endeavors videos(and implementing a state-machine) are way too complicated. Besides while implementing a state-machine, i would have to re-implement everything (which i don't wanna do). The basic requirements for wall-jumping are present in the script. But i still haven't wrote the code that will make it work. Also if you can't understand this properly, then you better not even comment cause in the past i have wasted a lot of time on Godot forum just cause of a guy who was giving me wrong solutions for the wall-jump problem. I tried following the tutorial from jean-make-games but he was doing it all wrong. His wall-jump was going in a a reversed L. I want it going in a arc like Super Mario. Remember I do not want any wrong advice and here is the player script:
extends KinematicBody2D
var up_direction = Vector2.UP
export (float) var gravity = 500.00
export (float) var second_jump_speed = -350
export (float) var wall_jump_knockback = 1500
export (float) var wall_jump_speed = -500
export (float) var max_speed = 250
export (float) var friction = 0.75
var velocity = Vector2.ZERO
export (float) var acceleration = 125
var can_double_jump : bool = false
var can_jump : bool = true
var have_double_jumped : bool = false
var slide_deacceleration_rate = 350.00
func _ready():
$slidecollisionshape.disabled = true
$secondjumpanimation.hide()
export (float) var jump_speed = -450.00
func _physics_process(delta :float) -> void:
var horizontal_direction = (
Input.get_action_strength("move_right")
- Input.get_action_strength("move_left")
)
if horizontal_direction != 0:
velocity.x += horizontal_direction * acceleration * delta
velocity.x = clamp(velocity.x, -max_speed, max_speed)
else:
velocity.x = lerp(velocity.x,0,friction)
velocity.y += gravity * delta
var next_to_wall = ($rightwalldetection.is_colliding() and $AnimatedSprite.flip_h == false) or ($leftwalldetection.is_colliding() and $AnimatedSprite.flip_h == true)
var is_walking = Input.is_action_pressed("walk") and horizontal_direction != 0 and is_on_floor()
var is_running = horizontal_direction != 0 and is_on_floor() and Input.is_action_pressed("run")
var is_idle = is_on_floor() and is_zero_approx(velocity.x)
var is_falling = not is_on_floor() and velocity.y > 0.0
var is_slow_running = is_on_floor() and horizontal_direction != 0.0 and (not is_walking or not is_running)
var is_jumping = Input.is_action_just_pressed("jump") and is_on_floor() and can_jump == true
var is_jump_cancelled = Input.is_action_just_released("jump") and velocity.y < 0.0
var is_right_wall_sliding = $AnimatedSprite.flip_h == false and is_falling and $rightwalldetection.is_colliding()
var is_left_wall_sliding = $AnimatedSprite.flip_h == true and is_falling and $leftwalldetection.is_colliding()
var is_wall_sliding = is_right_wall_sliding or is_left_wall_sliding
var is_wall_jumping = next_to_wall and Input.is_action_just_pressed("jump") and not is_on_floor()
var is_ledge_hanging = ($AnimatedSprite.flip_h == false and not $rightledgedetection.is_colliding() and $rightdetection.is_colliding()) or ($AnimatedSprite.flip_h == true and not $leftledgedetection.is_colliding() and $leftdetection.is_colliding())
var is_ledge_climbing = is_ledge_hanging and Input.is_action_just_pressed("up")
var is_double_jumping = Input.is_action_just_pressed("jump") and can_double_jump == true and have_double_jumped == false and not is_ledge_hanging
var is_sliding = velocity.x != 0 and horizontal_direction != 0 and Input.is_action_pressed("crouch") and is_on_floor()
if horizontal_direction < 0 and not is_ledge_hanging:
$AnimatedSprite.flip_h = true
elif horizontal_direction > 0 and not is_ledge_hanging:
$AnimatedSprite.flip_h = false
if is_running and not is_sliding:
max_speed = 400
acceleration = 200
$AnimatedSprite.play("run")
elif is_walking and not is_sliding:
max_speed = 100
acceleration = 100
$AnimatedSprite.play("walk")
elif is_slow_running and not is_sliding:
$AnimatedSprite.play("slow_run")
max_speed = 250
acceleration = 175
if is_sliding:
$CollisionShape2D.disabled = true
$slidecollisionshape.disabled = false
$AnimatedSprite.play("slide")
if $AnimatedSprite.flip_h == false:
velocity.x = clamp(velocity.x,5.833,400)
velocity.x -= slide_deacceleration_rate * delta
elif $AnimatedSprite.flip_h == true:
velocity.x = clamp(velocity.x,-400,-5.833)
velocity.x += slide_deacceleration_rate * delta
else:
$slidecollisionshape.disabled = true
$CollisionShape2D.disabled = false
if is_wall_sliding:
velocity.y = 100
$AnimatedSprite.play("wallslide")
else:
velocity.y += gravity * delta
if is_ledge_hanging:
$AnimatedSprite.play("ledge_hang")
velocity.y = 0
velocity.x = 0
gravity = 0
max_speed = 0
if is_ledge_climbing:
$AnimatedSprite.play("ledge_climb")
velocity.y = -350
gravity = 500
max_speed = 250
if is_idle:
$AnimatedSprite.play("idle")
if is_falling and not is_wall_sliding and not is_wall_jumping:
$AnimatedSprite.play("falling")
if is_jumping:
velocity.y = jump_speed
$AnimatedSprite.play("jump")
elif is_double_jumping:
velocity.y = second_jump_speed
$AnimatedSprite.play("jump")
$secondjumpanimation.show()
have_double_jumped = true
elif is_jump_cancelled:
velocity.y = 0.0
else:
$secondjumpanimation.hide()
if not next_to_wall and is_falling:
can_double_jump = true
else:
can_double_jump = false
if is_on_floor():
can_jump = true
have_double_jumped = false
else:
can_jump = false
velocity = move_and_slide(velocity, up_direction)