Skip to content

Instantly share code, notes, and snippets.

@Tonetfal
Created July 7, 2025 12:40
Show Gist options
  • Save Tonetfal/0e6c1e0878eb62f92885c698602d1110 to your computer and use it in GitHub Desktop.
Save Tonetfal/0e6c1e0878eb62f92885c698602d1110 to your computer and use it in GitHub Desktop.
CameraModifier_Crouching
// Header
#include "Camera/CameraModifier.h"
#include "MESS_CameraModifier_Crouching.generated.h"
/**
*
*/
UCLASS()
class MESS_API UMESS_CameraModifier_Crouching
: public UCameraModifier
{
GENERATED_BODY()
public:
//~UCameraModifier Interface
virtual bool ModifyCamera(float DeltaTime, FMinimalViewInfo& InOutPOV) override;
//~End of UCameraModifier Interface
UFUNCTION(BlueprintNativeEvent)
float GetBlendSpeed() const;
private:
UPROPERTY(EditAnywhere)
float GroundBlendSpeed = 15.f;
UPROPERTY(EditAnywhere)
float AirBlendSpeed = 40.f;
float CurrentEyeHeight = 0.f;
};
// Source
#include "Player/Camera/MESS_CameraModifier_Crouching.h"
#include "GameFramework/Character.h"
#include "GameFramework/CharacterMovementComponent.h"
bool UMESS_CameraModifier_Crouching::ModifyCamera(float DeltaTime, FMinimalViewInfo& InOutPOV)
{
const AActor* ViewTarget = GetViewTarget();
const auto* ViewTarget_Pawn = Cast<APawn>(ViewTarget);
if (!IsValid(ViewTarget_Pawn))
{
return Super::ModifyCamera(DeltaTime, InOutPOV);
}
const FVector EyesViewLocation = ViewTarget_Pawn->GetPawnViewLocation();
if (FMath::IsNearlyEqual(CurrentEyeHeight, EyesViewLocation.Z, 1.f))
{
CurrentEyeHeight = EyesViewLocation.Z;
}
else
{
const float BlendSpeed = GetBlendSpeed();
CurrentEyeHeight = FMath::FInterpTo(CurrentEyeHeight, EyesViewLocation.Z, DeltaTime, BlendSpeed);
}
InOutPOV.Location.Z = CurrentEyeHeight;
return true;
}
float UMESS_CameraModifier_Crouching::GetBlendSpeed_Implementation() const
{
const AActor* ViewTarget = GetViewTarget();
const auto* ViewTarget_Character = Cast<ACharacter>(ViewTarget);
if (IsValid(ViewTarget_Character))
{
if (ViewTarget_Character->GetCharacterMovement()->IsFalling())
{
return AirBlendSpeed;
}
}
return GroundBlendSpeed;
}
@MajorTomAW
Copy link

🗿 good stuff

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment