So I’m trying to make crouch and prone mechanics in ue 5.6 cpp.I looked on the internet and couldn’t find any good tutorial about this topic especially for cpp. I made something like this but this code doesn’t work for switching from crouch to prone.
void APlayerCharacter::SetStance(EStanceState NewStance)
{
if (CurrentStance == NewStance) return;
switch (CurrentStance)
{
case EStanceState::Crouch:
UnCrouch();
break;
case EStanceState::Prone:
UnProne();
break;
}
CurrentStance = NewStance;
switch (CurrentStance)
{
case EStanceState::Stand:
GetCharacterMovement()->MaxWalkSpeed = MovementSpeed;
break;
case EStanceState::Crouch:
Crouch();
GetCharacterMovement()->MaxWalkSpeed = CrouchSpeed;
break;
case EStanceState::Prone:
Prone();
break;
}
}
void APlayerCharacter::OnCrouchPressed()
{
const EStanceState Target = (CurrentStance == EStanceState::Crouch) ? EStanceState::Stand : EStanceState::Crouch;
SetStance(Target);
}
void APlayerCharacter::OnPronePressed()
{
if (CurrentStance != EStanceState::Stand && !CanStand())
{
UE_LOG(LogTemp, Warning, TEXT("Can't stand!!"));
return;
}
const EStanceState Target = (CurrentStance == EStanceState::Prone) ? EStanceState::Stand : EStanceState::Prone;
SetStance(Target);
}
bool APlayerCharacter::CanStand() const
{
FVector Start = GetActorLocation();
FVector End = Start + FVector(0.0f, 0.0f, 190.f);
FHitResult Hit;
FCollisionQueryParams CollisionParams;
CollisionParams.AddIgnoredActor(this);
return !GetWorld()->LineTraceSingleByChannel(Hit, Start, End, ECollisionChannel::ECC_Visibility, CollisionParams);
}
void APlayerCharacter::Prone()
{
UE_LOG(LogTemp, Warning, TEXT("Character Height: %f"), GetCapsuleComponent()->GetUnscaledCapsuleHalfHeight());
GetCapsuleComponent()->SetCapsuleHalfHeight(30.f, true);
GetMesh()->SetRelativeLocation(FVector(0.f, 0.f, -35.f));
GetCharacterMovement()->MaxWalkSpeed = ProneSpeed;
UE_LOG(LogTemp, Warning, TEXT("Proning!!"));
UE_LOG(LogTemp, Warning, TEXT("Character Height: %f"), GetCapsuleComponent()->GetUnscaledCapsuleHalfHeight());
}
void APlayerCharacter::UnProne()
{
GetCapsuleComponent()->SetCapsuleHalfHeight(93.f);
GetMesh()->SetRelativeLocation(FVector(0.f, 0.f, -95.f));
UE_LOG(LogTemp, Warning, TEXT("Unproning!!"));
}
Is there any other way to do it?