Skip to content

Instantly share code, notes, and snippets.

@Plimsky
Last active January 8, 2025 20:40
Show Gist options
  • Save Plimsky/925aacdcad0e9d259f690efe562d7bdf to your computer and use it in GitHub Desktop.
Save Plimsky/925aacdcad0e9d259f690efe562d7bdf to your computer and use it in GitHub Desktop.
Add rotation inertia to the camera of the UE Motion FPS plugin
void UMotionCameraComponent::GetCameraView(float DeltaTime, FMinimalViewInfo& DesiredView)
{
// ...
for (const TTuple<FString, UE::Math::TRotator<double>> & StaticRotationOffset: CameraStaticRotationOffsets)
{
UpdatedCameraRotation = FRotator(FQuat(UpdatedCameraRotation) * FQuat(StaticRotationOffset.Value));
}
UpdatedCameraRotation = FRotator(FQuat(UpdatedCameraRotation) * FQuat(CameraRotatorFromCurves));
// Apply inertia effect based on camera rotation
if (bFirstInitialization)
{
CurrentRotation = UpdatedCameraRotation;
bFirstInitialization = false;
}
if (bEnableCameraInertia && CurrentRotation.Equals(UpdatedCameraRotation, 0.01f) == false)
{
// Interpolate towards the target rotation
CurrentRotation = FMath::RInterpTo(CurrentRotation, UpdatedCameraRotation, DeltaTime, CameraInertiaStrength);
}
else
{
CurrentRotation = UpdatedCameraRotation;
}
DesiredView.Rotation = CurrentRotation;
SetWorldRotation(CurrentRotation);
// ...
}
class MOTIONCORE_API UMotionCameraComponent : public UCameraComponent
{
GENERATED_BODY()
//...
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Configuration")
float CameraInertiaStrength = 1.0f;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Configuration")
bool bEnableCameraInertia = false;
UPROPERTY()
bool bFirstInitialization = true;
UPROPERTY()
FRotator CurrentRotation = FRotator::ZeroRotator;
//...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment