How do I create procedural camera bobbing?

I am wondering how I would execute camera bobbing without a baked animation in Unreal Engine. Commonly in Unity, we oscillate the camera’s transform using Sine and Cosine trigonometric functions. I am wondering the best way to execute camera bobbing procedurally in Unreal Engine using a similar method. I have experience in C++ so a code example would be appreciated although an explanation would be just as much appreciated.

In Unreal Engine usually it is better to implement things in blueprints if they’re possible. Here’s a video on how to achieve your goal using them: https://m.youtube.com/watch?v=o1g22erv-2Y

1 Like

If you want fine control over your camera shake values I’m going to give you a C++ example.

Let’s add a variable for storing the “spawned” camera shake because we need it to stay persistent and we can simply change its values at runtime.

UPROPERTY()
class ULegacyCameraShake* HeadBobCamShakePtr = nullptr;

You can start your camera shake like this, I’m doing this in the character, OscillationDuration is -1 so it never stops.

if(GetController() && GetController()->IsLocalController())
{
	APlayerCameraManager* CamManager = GetController<APlayerController>()->PlayerCameraManager;
	UCameraShakeBase* CamShake = CamManager->StartCameraShake(UCameraShakeBase::StaticClass(), 1.f, ECameraShakePlaySpace::CameraLocal, FRotator::ZeroRotator);
	HeadBobCamShakePtr = Cast<ULegacyCameraShake>(CamShake);
    HeadBobCamShakePtr->OscillationDuration = -1.f; //indefinite
}

Now you can manipulate the camera shake values at runtime like this:

HeadBobCamShakePtr->RotOscillation.Pitch.Amplitude= ?
HeadBobCamShakePtr->RotOscillation.Pitch.Frequency = ?
HeadBobCamShakePtr->ShakeScale = ?

You can smoothly interpolate those values based on sprinting, walking, taking damage etc.

2 Likes

Thank you for the support as soon as I make it home I am going to test this solution on an FPS controller.

1 Like

The video shown is good although coming from a C#, LUA, and C++ background it is quite difficult to adapt to a non-text-based language.