I’m generating a character’s capsule collision and radius from the mesh at BeginPlay(). I can walk around fine using the associated animations as well as crouch, but it looks like my characters crouch height pops the mesh upward, and when the character stands up after the mesh is floating above the ground.
I’m not sure exactly what I’m getting wrong, whether there’s an issue with how I’m generating the collision, the halfHeight or if I need to augment the crouch() and uncrouch() functions with custom code. It seems to be related to the halfHeight getting overwritten because the debug output I have shows it matching my original value of 90 and then after uncrouch() goes back to the default of 88.
/* Create Capsule Collision */
if (GetMesh() && GetMesh()->SkeletalMesh != nullptr)
{
// Find this character's mesh bounds and scale.
const FBoxSphereBounds MeshBounds{ GetMesh()->SkeletalMesh->GetBounds() };
const FVector Scale3D{ GetMesh()->RelativeScale3D };
// set collision cylinder appropriately to mesh size
newRadius = { ((MeshBounds.BoxExtent.X + MeshBounds.BoxExtent.Y) / 2.0f) * 0.5f * FMath::Max(Scale3D.X, Scale3D.Y) };
newHalfHeight = { MeshBounds.BoxExtent.Z * Scale3D.Z };
// set the currently in-use cylinders
GetCapsuleComponent()->SetCapsuleSize(newRadius, newHalfHeight);
GetCapsuleComponent()->SetCapsuleHalfHeight(newHalfHeight);
GetCapsuleComponent()->SetRelativeScale3D(FVector(1.0f, 1.0f, 1.0f));
// Set Character Half Height for crouching
GetCharacterMovement()->CrouchedHalfHeight = newHalfHeight;
// Update the Mesh's relative location, taking into account foot-origin vs. center-origin characters.
FVector NewRelativeLocation{ GetClass()->GetDefaultObject<AMWCharacterBase>()->GetMesh()->RelativeLocation };
NewRelativeLocation.Z = -(GetCapsuleComponent()->GetScaledCapsuleHalfHeight());
GetMesh()->SetRelativeLocation(NewRelativeLocation);
UE_LOG(LogTemp, Warning, TEXT("Capsule Half Height %f"), GetCapsuleComponent()->GetUnscaledCapsuleHalfHeight());
UE_LOG(LogTemp, Warning, TEXT("Capsule Radius %f"), GetCapsuleComponent()->GetUnscaledCapsuleRadius());
UE_LOG(LogTemp, Warning, TEXT("Character CrouchedHalfHeight %f"), GetCharacterMovement()->CrouchedHalfHeight);
}
Here’s what the logs print out
LogTemp: Warning: Capsule Half Height 90.046127
LogTemp: Warning: Capsule Radius 28.170179
LogTemp: Warning: Character CrouchedHalfHeight 90.046127
Before first crouch. As you can see the character is standing on the floor and is comfortably within the ceiling of the frame.
First crouch, I noticed that the capsule collision isn’t getting resized, I presumed the crouch() function would do this, so perhaps it’s an issue with my setup.
Standing after first crouch
2nd Crouch