Godot Version
4.7
Question
I have done what I think works for me and what should help me from coming across issues later on with a huge list of if , then statements. The issue that I am having currently deals with crouching. I have looked through a few different crouching posts here and I am unsure how to handle my specific issue.
What I know:
I have a crouching statement that changes the characters animation to crouch or to the opposite of it when I hit the crouch button.
I have a ray cast that when it detects collision above it that would stop the character from standing up it forces the crouch to stay as true and won’t let the player stand up.
**
What I can’t resolve:**
I want it setup in a way that if I hit the crouch button while under something that would stop the player from standing I want the player to stand up once they exit the thing they are crouched under.
I know that the ray cast forces crouch to true if collision occurs so that’s what is currently forcing me to stay crouched even if I hit the crouch button again while under something. I know it’s overwritten by the ray cast and is there a way to stop it from doing so. Am I able to have some kind of setup that would allow me to “preemptively” hit the crouch to stand up even while still under something that won’t let me and it stands the player character up once you exit the crouching zone?
Hopefully the above is enough info.
The code:
The code I have for crouching is under the handle_crouching function but i wanted to make sure the entire player character code was provided just in case there is some info I didn’t provide in the above text.
extends CharacterBody2D
@export var movement_speed : float = 100
@export var sprint_speed : float = 200
@export var jump_height : float = 100
var jump_count : float = 0
var max_jumps : float = 2
var gravity : float = 980
var current_speed : float = 0
var can_jump : bool = false
var crouching : bool = false
var sprinting : bool = false
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta: float) -> void:
handle_movement_physics(delta)
handle_crouching()
handle_sprinting()
move_and_slide()
func handle_crouching():
if Input.is_action_just_pressed("crouch"):
crouching = !crouching
if $CrouchDetector.is_colliding():
crouching = true
if crouching:
sprinting = false
$CrouchedAnimation.show()
$StandingAnimation.hide()
$PlayerCollision.scale.y = 0.5
$PlayerCollision.position.y = 15
else:
$StandingAnimation.show()
$CrouchedAnimation.hide()
$PlayerCollision.scale.y = 1.0
$PlayerCollision.position.y = 0
func handle_movement_physics(delta):
velocity.x = 0
if not is_on_floor():
velocity.y += gravity * delta
else:
jump_count = 0
if Input.is_action_pressed("move_left"):
velocity.x -= current_speed
if Input.is_action_pressed("move_right"):
velocity.x += current_speed
#jumping and controling if the character can jump more than once depending on the jump_count
if Input.is_action_just_pressed("jump"):
#if the character is on the floor OR they have less jumps left than the max_jumps then they can still jump.
if is_on_floor() and jump_count < max_jumps:
velocity.y = -jump_height
jump_count += 1
func handle_sprinting():
current_speed = movement_speed
if sprinting == true:
current_speed = sprint_speed
if Input.is_action_pressed("sprint"):
sprinting = true