Godot Version
4.3
Question
I Need some help with some code for movement. I've never really coded before so I made it by mashing tutorials together, AI, And some problem solving. This has made it sort of crappy. Can you guys help me clean it up and fix some problems to do with the jumping mechanics. Currently when moving on the ground the jump count doesn't reset properly and I can only jump once until I stop moving.
extends CharacterBody2D
#Basic movement constants
const max_speed = 300
const SPEED = 300
const JUMP_VELOCITY = -360
const Friction_ground = 50
const Friction_air = 10
const acc = 30
const gravity = 10
const acc_degration = 0.5
#dubble jump variables
var jump_count = 0
@export var max_jump = 1
#wall Jump/slide
const wall_jump_pushback = 300
const wall_slide_gravity = 120
var is_wall_sliding = false
var wall_jump_pushback_timer = 0.0
#raycasting for wall jumps
@onready var raycast_left = $RayCastLeft
@onready var raycast_right = $RayCastRight
@onready var Enable_Movement = $EnableMovement
var velocity_count_1 = 1
var velocity_count_2 = 1
var dash_speed = 4
var is_dashing = false
func gravity_2(delta):
if not is_on_floor() and not is_wall_sliding:
velocity += get_gravity() * delta
else:
velocity.y = max(velocity.y,0)
func add_friction():
if is_on_floor():
jump_count = 0
if not Input.is_action_pressed("left") and not Input.is_action_pressed("right"):
if velocity.x > 0:
velocity.x = max(velocity.x - Friction_ground, 0)
elif velocity.x < 0:
velocity.x = min(velocity.x + Friction_ground, 0)
jump_count += 1
if not is_on_floor():
if not Input.is_action_pressed("left") and not Input.is_action_pressed("right"):
if velocity.x > 0:
velocity.x = max(velocity.x - Friction_air, 0)
elif velocity.x < 0:
velocity.x = min(velocity.x + Friction_air, 0)
velocity.y += gravity
func accelerate(direction):
var remaining_speed = max_speed - abs(velocity.x)
var acceleration_reduction = 1 - (remaining_speed / max_speed) * acc_degration
var effective_acc = acc * acceleration_reduction
velocity.x += direction.x * effective_acc
velocity.x = clamp(velocity.x, -max_speed, max_speed)
func input():
var input_dir = Vector2.ZERO
input_dir.x = Input.get_axis("left","right")
input_dir = input_dir.normalized()
return input_dir
func player_movemet():
move_and_slide()
func _physics_process(delta: float) -> void:
# applying gravity
gravity_2(delta)
# floor based acceleration/jump logic
handle_normal_jump()
handle_wall_jump_and_slide(delta)
#horizoפntal movment
var input_dir : Vector2 = input()
if input_dir != Vector2.ZERO:
accelerate(input_dir)
else:
add_friction()
player_movemet()
func handle_wall_jump_and_slide(delta):
var on_left_wall = raycast_left.is_colliding()
var on_right_wall = raycast_right.is_colliding()
if (on_left_wall or on_right_wall) and not is_on_floor():
if is_wall_sliding and Input.is_action_just_pressed("jump"):
if on_left_wall:
if not Input.is_action_pressed("left"):
velocity.x = 0
else:
velocity.x = 600
elif on_right_wall:
if not Input.is_action_pressed("right"):
velocity.x = 0
else:
velocity.x = -600
velocity.y = JUMP_VELOCITY
wall_jump_pushback_timer = 1
is_wall_sliding = false
Enable_Movement = 0.5
if (on_left_wall or on_right_wall) and not is_on_floor():
if on_left_wall:
if Input.is_action_pressed("left"):
is_wall_sliding = true
jump_count = 0
else:
is_wall_sliding = false
elif on_right_wall:
if Input.is_action_pressed("right"):
is_wall_sliding = true
jump_count = 0
else:
is_wall_sliding = false
# If wall sliding, we apply wall slide gravity
if is_wall_sliding:
velocity.y = min(velocity.y + (wall_slide_gravity * delta), wall_slide_gravity)
# Wall Jump (Left Wall or Right Wall)
else:
is_wall_sliding = false
func handle_normal_jump():
if Input.is_action_just_pressed("jump") and jump_count < max_jump:
velocity.y = JUMP_VELOCITY + 10
jump_count += 1
func _on_hurtbox_body_entered(body: Node2D) -> void:
if body.is_in_group("Enemy_group"):
print("Enemy_group",body.damage_amount)
HealthManager.decrease_health(body.damage_amount)```