4.4
I am having trouble with a crouching system. I am currently using a ShapeCast2D to check if anything is colliding but i am using a while loop and it should terminate but my game keeps crashing when i run it so i am pretty sure that it is the while loop but it won’t terminate so here’s the code.
extends CharacterBody2D
var is_crouching = false
var can_uncrouch = true
var can_crouch = true
var jump_count = 0
var speed = 600
var jump_velocity = -600
func _process(_delta: float) → void:
$LightHolder.look_at(get_global_mouse_position())
if $UncrouchChecker:
print(“fjfjfjfjfgjfjfjfhfjfjfhgjfhgjfhgjfhgjfhgjfhgjfhgjfhg”) # Ignore this
#while $UncrouchChecker.collide_with_bodies(StaticBody2D):
can_uncrouch = false
can_uncrouch = true
if is_crouching == false:
$Pupil/EyeAnimations.play("Looking")
else:
$Pupil/EyeAnimations.stop(false)
if Input.is_action_pressed("LeftClick"):
$LightHolder.visible = true
else:
$LightHolder.visible = false
func _physics_process(delta: float) → void:
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
if Input.is_action_pressed("ui_accept") and is_on_floor(): #Jump
velocity.y = jump_velocity
jump_count = 1
elif Input.is_action_just_pressed("ui_accept") and jump_count == 1: # Double Jump
velocity.y = jump_velocity
jump_count = 0
# Handles crouching
if Input.is_action_pressed("ui_down"): # Crouch
is_crouching = true
speed = 300
$Body.scale.y = 0.145
$Pupil.scale.y = 0.145
$Collision.scale.y = 0.748
elif Input.is_action_just_released("ui_down") and can_uncrouch == true: # Uncrouch
is_crouching = false
speed = 600
$Body.scale.y = 0.39
$Pupil.scale.y = 0.376
$Collision.scale.y = 1.728
var direction := Input.get_axis("ui_left", "ui_right")
if direction:
velocity.x = direction * speed
else:
velocity.x = move_toward(velocity.x, 0, speed)
if Input.is_action_pressed("ui_left") and is_crouching == false:
$PlayerAnimations.play("Left")
elif Input.is_action_pressed("ui_right") and is_crouching == false:
$PlayerAnimations.play("Right")
else:
$PlayerAnimations.stop(false)
move_and_slide()
and here’s a screenshot of my main issue with this system
the player keeps getting stuck in the walls and floor when you uncrouch in a crouching area