smooth Zoom camera on crouch toggle c++ (TimeLine/EnhancedInput)

I want to replicate this blueprint from a tutorial I’m following in C++
image
what this does is zoom out the camera when crouching and zoom in when uncrouching but the problem I found is when I try to implement it into the crouch function
I have to add a float parameter to it but I can’t do that as I can’t “Bindaction” to it anymore
so I tried to do it this way :

void ATheLastGameDevCharacter::ToggleCrouch()
{
	isCrouched = !isCrouched;
	CrouchCameraZoom();
	if (isCrouched)
	{
		GetCharacterMovement()->MaxWalkSpeed = 350.0f;

		CrouchCameraTimeline.PlayFromStart();
	}
	else
	{
		GetCharacterMovement()->MaxWalkSpeed = 500.0f;

		CrouchCameraTimeline.ReverseFromEnd();
	}
}

void ATheLastGameDevCharacter::CrouchCameraZoom()
{
	if (CrouchCameraCurve)
	{
		// Bind the interpolation function
		InterpFunction.BindUFunction(this, FName("CrouchCameraZoomUpdate"));
		CrouchCameraTimeline.AddInterpFloat(CrouchCameraCurve, InterpFunction, FName("Zoom"));
		// Set the timeline playback settings
		CrouchCameraTimeline.SetPlayRate(1.0f);
		CrouchCameraTimeline.SetLooping(false);
	}
}

void ATheLastGameDevCharacter::CrouchCameraZoomUpdate(float Value)
{
	// Update the arm length of the camera boom
	CameraBoom->TargetArmLength = FMath::Lerp(550, 400, Value);
}

This does work but it’s not smooth at all it just snaps to place!

any help is very appreciated ^_^!

this is a curve for my camera boom,
you can set your curve to be starting from 550 to 400 with your desired time(maybe like 1 second i guess)
then the CrouchCameraZoomUpdate function will be like this

void ATheLastGameDevCharacter::CrouchCameraZoomUpdate(float Value)
{
// Update the arm length of the camera boom
CameraBoom->TargetArmLength = Value;
}

also, Change PlayFromStart and ReverseFromEnd to just Play and Reverse
Because the timeline will do the interpolation, if you play the timeline from start or end, it will make your cameraboom to teleport

check these and see if it works for you!

Changing the curve and the function have the exact same effect and changing PlayFromStart and ReverseFromEnd to just Play and Reverse doesn’t workand it even removes the snaping so i get no effect at all

also i added UE_LOG(LogTemp, Warning, TEXT("%f"), &Value); and i get
image
and I noticed that every time I press the crouch key, it prints one more warning, so basically the first key press it prints 1, the second time 2 then 3 …

Welp i fixed it and all i had to do is change it from FTimeline to UTimelineComponent (which i did before and still didn’t work then ???) the weird thing is it still prints 0 but at constant rate and not the same behavior as before

It has been a while, but for future visitors:
UE_LOG(LogTemp, Warning, TEXT(“%f”), &Value); The ampersand (&) should go.
It is also probably better to move the call to CrouchCameraZoom() to Beginplay().

1 Like