Crouching problem

Godot Version

4.2.1

Question

Hello! I’m trying to add crouching to my FPS game, but I have one problem: when I use RayCast3D node (I tried ShapeCast3D, but it ignores collisions) to prevent player from standing up under low object all works fine, except one thing: if I stand close to low object, hold “W” key and press “crouch” for a small time, my player starts to bug out (clipping into the wall). Can someone tell me better way to make crouching? Currently I use animations in AnimationPlayer node (crouch, uncrouch). Also tried enabling/disabling two different collision shapes, same problem.

Hello !

Can it be that the starting position of your RayCast3D is inside the object you want to collide with when your too close to it ?

Check the hit_from_inside property of you RayCast3D : RayCast3D — Godot Engine (stable) documentation in English

Actually, it’s not, because my raycast comes from the bottom of the character collision

It sounds like you’re not detecting if an object is above your character until after you hit the crouch key. Can you show that part of your code?

Thanks for helping, the solution has been found

Would you mind elaborating on your solution?

@GodofGrunts you’re exactly right about what I’m doing. I’m blanking on a more clever way to handle this, do you have any suggestions?

	var speed = SPEED
	if Input.is_action_pressed("Crouch"):
		speed = CROUCHSPEED
		if !crouched: 
			$AnimationPlayer.play("Crouch")
			crouched = true
	else: 
		if crouched: 
			if not $HeadRayCast3D.is_colliding():
				$AnimationPlayer.play("UnCrouch")
				crouched = false