Hello!
I would like to create a custom crouch function in blueprints mainly to change the size of the capsule component like it does in the crouch function if there is a way that’s possible?
Any help will be appreciated, thank you!
Hello!
I would like to create a custom crouch function in blueprints mainly to change the size of the capsule component like it does in the crouch function if there is a way that’s possible?
Any help will be appreciated, thank you!
Thanks for your answer I’ve just implemented it and it works very well probably better than the standard function. However my game is a third person shooter and when the crouch is enabled the character sinks through the floor for some reason. If you know a way to fix this it world be much appreciated. Also is there a way to make it so it’s event based cos using event tick I think is a bit unnecessary when you might only be crouching for 5 minutes per level. Thank you very much!
I’m actually having the same issue too^ I really want to know how to prevent my character from sinking
For those having the character-sinking issue when implementing the custom crouch, its because the mesh component is attached to the very middle of the capsule, and when you change the capsule height the point the mesh is parented to changes as well. How I solved it was to AddRelativeLocation to the Mesh component with a + offset in Z when crouching, and a - offset when uncrouching. Hypothetically (CapsuleHeight - CrouchCapsuleHeight)/2 would give you the needed offset, but for my test animations at least I needed to boost that offset another 5 units.
in the .h
UPROPERTY(EditAnywhere, Category = "Movement | Crouch", meta = (AllowPrivateAccess = "true", Tooltip = "Value is in centimeters"))
float DefaultCapsuleHeight = 0;
UPROPERTY(EditAnywhere, Category = "Movement | Crouch", meta = (AllowPrivateAccess = "true", Tooltip = "Value is in centimeters"))
float DefaultCapsuleRadius = 0;
UPROPERTY(EditAnywhere, Category = "Movement | Crouch", meta = (AllowPrivateAccess = "true", Tooltip = "Value is in centimeters"))
float CrouchCapsuleHeight = 0;
UPROPERTY(VisibleAnywhere, Category = "Movement | Crouch", meta = (AllowPrivateAccess = "true", Tooltip = "Value is in centimeters"))
float CurrentCapsuleHeight = 0;
UPROPERTY(EditAnywhere, Category = "Movement | Crouch", meta = (AllowPrivateAccess = "true", Tooltip = "Value is in centimeters"))
float CrouchSpeed = 0;
bool bCrouch = false;
FORCEINLINE void Crouching() { bCrouch = !bCrouch; };
in the .cpp
void AHero::BeginPlay()
{
Super::BeginPlay();```
DefaultCapsuleRadius = GetCapsuleComponent()->GetUnscaledCapsuleRadius();
DefaultCapsuleHeight = GetCapsuleComponent()->GetUnscaledCapsuleHalfHeight();
CurrentCapsuleHeight = DefaultCapsuleHeight;
CrouchCapsuleHeight = DefaultCapsuleHeight / 2;
}
void AHero::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
CrouchSpeed = DeltaTime * 5;
if (bCrouch && CurrentCapsuleHeight > CrouchCapsuleHeight)
{
CurrentCapsuleHeight = FMath::Lerp(CurrentCapsuleHeight, CrouchCapsuleHeight,
CrouchSpeed);
GetCapsuleComponent()->SetCapsuleSize(DefaultCapsuleRadius, CurrentCapsuleHeight,
true);
}
if (!bCrouch && CurrentCapsuleHeight < DefaultCapsuleHeight)
{
CurrentCapsuleHeight = FMath::Lerp(CurrentCapsuleHeight, DefaultCapsuleHeight,
CrouchSpeed);
GetCapsuleComponent()->SetCapsuleSize(DefaultCapsuleRadius, CurrentCapsuleHeight,
true);
}
}