Toggle crouching does not work c++

Hi my crouch function does not seem to work. When i press the crouch input. Nothing happens. The capsule size does not change. Is my code correct?

Player.h

void ToggleCrouch();

Player.cpp

APlayer::APlayer()
{
	  PrimaryActorTick.bCanEverTick = true;
	
     GetCharacterMovement()->GetNavAgentPropertiesRef().bCanCrouch = true;
}

void APlayer::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

    PlayerInputComponent->BindAction("CrouchToggle", IE_Pressed, this, &APlayer::ToggleCrouch);
}

void APlayer::ToggleCrouch()
{
	if (GetCharacterMovement()->IsCrouching())
	{
		UnCrouch();
	}
	else
	{
		Crouch();
	}
}
1 Like

OK i figured out the solution. Ill post it just in case anyone else runs into the same issue.

void APlayer::ToggleCrouch()
{
	if (GetCharacterMovement()->IsCrouching())
	{
		UnCrouch();
		
	}
	else
	{
		Crouch();
		GetCharacterMovement()->GetNavAgentPropertiesRef().bCanCrouch = true;
		
	}
}

Move this GetCharacterMovement()->GetNavAgentPropertiesRef().bCanCrouch = true; from the constructor
into the Crouch function:

If the issue is fixed please mark it as correct to close it :slight_smile: