Post

Camera Rotation Smoothing With Motion Plugin On Unreal Engine

Camera Rotation Smoothing With Motion Plugin On Unreal Engine

Context

I really like the plugin True First Person developped by Kia Armani. It’s a great plugin that allows you to create a first person camera with a lot of features with nice and smooth camera movements.

In a project where I used this plugin, I wanted make the camera rotation smoother. I found a way to achieve this effect and I wanted to share it with you.

You’ll have to modify the UMotionCameraComponent class from the plugin. The version I use is the 1.6.x.

The code

MotionCameraComponent.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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;

  //...
}

MotionCameraComponent.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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);

    // ...
}

Explanation

This part is the original code from the plugin. It’s used to apply the camera rotation from the curves and the static rotation offsets.

1
2
3
4
5
6
for (const TTuple<FString, UE::Math::TRotator<double>> & StaticRotationOffset: CameraStaticRotationOffsets)
{
    UpdatedCameraRotation = FRotator(FQuat(UpdatedCameraRotation) * FQuat(StaticRotationOffset.Value));
}

UpdatedCameraRotation = FRotator(FQuat(UpdatedCameraRotation) * FQuat(CameraRotatorFromCurves));

The next part is new. It’s used to apply the inertia effect on the camera rotation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 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;
}
  • For the first initialization, we need to initialize CurrentRotation with the UpdatedCameraRotation.

  • If there is rotation delta between the current and the updated rotation above 0.01f (you may change this value if you want)

    • Then the camera will interpolate towards the target rotation with CameraInertiaStrength as the interpolation speed.

In case this article become outdated, you may also want to check the Gist with the latest changes: https://gist.github.com/Plimsky/925aacdcad0e9d259f690efe562d7bdf

Conclusion

It’s simple as that! You can now have a smoother camera rotation with the True First Person plugin.

You may want to tweak the CameraInertiaStrength value to match your needs.

Have a fantastic day!

This post is licensed under CC BY 4.0 by the author.

Trending Tags