RInterpTo/Constant not working properly.

I have a code that needs to reset my camera’s rotation under some conditions:

void ADefaultPlayerController::CameraRotationReset()
{
	if (UKismetMathLibrary::EqualEqual_RotatorRotator(GameplayCamera->GetActorRotation(), FRotator(0,0,0), 0.1))
	{
		GetWorldTimerManager().ClearTimer(CameraResetTimer);
	}

	GameplayCamera->SetActorRotation(
		UKismetMathLibrary::RInterpTo(GameplayCamera->GetActorRotation(), FRotator::ZeroRotator, *DeltaSeconds, CameraRotateSpeed*5));
}

Here when this functions starts by the timer GetWorldTimerManager().SetTimer(CameraResetTimer, this, &ADefaultPlayerController::CameraRotationReset, 0.01f, true); it won’t stop because it doesn’t update the camera’s rotation (as you see above, it needs to stop when camera rotation is 0). If I set the rotation by manually within editor at runtime, algorithm continues to work as expected and gets stuck again at this interp.

I’m transfering this code from BP to C++ and the BP version works perfectly:

And that is not only place I use RInterpTo. There is one more of it which rotates the camera and it works fine.

What I tried:
-There is nothing that interrupts i.e forces back the camera when it should go to 0 rotation.
-Tried both FMath/UKismetMath and RInterpTo/RInterpToConstant.
-AI suggested that there could be a math problem so normalize your rotation before calculations, didn’t change anything.
-Revised the (whole) code several times, it’s same as the BP one.

All code is in the Player Controller and there is no code (in another class) that alters camera or any other related component.

Header

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

include “CoreMinimal.h”
include “GameFramework/PlayerController.h”
include “Camera/CameraActor.h”
include “PlayerControllerCPP.generated.h”

UCLASS()
class Your_API APlayerControllerCPP : public APlayerController
{
GENERATED_BODY()

public:
	APlayerControllerCPP();

UPROPERTY(BlueprintReadWrite,EditAnywhere)
FTimerHandle CameraResetTimer;

UPROPERTY(BlueprintReadWrite, EditAnywhere)
float CameraRotateSpeed = 3;

UPROPERTY(BlueprintReadWrite, EditAnywhere)
TObjectPtr<ACameraActor> GameplayCamera;

void CameraRotationReset();

UFUNCTION(BlueprintCallable)
void StartReset();

};

cpp

include “PlayerControllerCPP.h”

include “Kismet/KismetMathLibrary.h”
include “GameFramework/Actor.h”

APlayerControllerCPP::APlayerControllerCPP()
{
GameplayCamera = CreateDefaultSubobject(TEXT(“Camera”));

}

void APlayerControllerCPP::CameraRotationReset()
{
GEngine->AddOnScreenDebugMessage(4, 4, FColor::Orange, GameplayCamera->GetActorRotation().ToString());

if (UKismetMathLibrary::EqualEqual_RotatorRotator(GameplayCamera->GetActorRotation(), FRotator(0, 0, 0), 0.1))
{
	GEngine->AddOnScreenDebugMessage(1, 2, FColor::Green, "RESET");

	GetWorldTimerManager().ClearTimer(CameraResetTimer);
}

GEngine->AddOnScreenDebugMessage(1, 2, FColor::Yellow, "CameraRotationReset Resetting");


GameplayCamera->SetActorRotation(
	UKismetMathLibrary::RInterpTo(GameplayCamera->GetActorRotation(), FRotator::ZeroRotator, GetWorld()->GetDeltaSeconds(), CameraRotateSpeed * 5));

}

void APlayerControllerCPP::StartReset()
{

GetWorldTimerManager().SetTimer(CameraResetTimer, this, &APlayerControllerCPP::CameraRotationReset, 0.01f, true);

}

It works fine but your camera has to be of type ACameraActor and not be a component.

And a quick bp init in the controller

Maybe you forgot to update delta time if you are just using a float?

1 Like

Oh yes the problem was about delta seconds, a rookie mistake. I was passing the delta to functions as reference directly from Tick. When I checked it inside that reset function found out that something has corrupting the delta, it had some random negative values. I’ve changed the code to pass delta as copy and problem fixed.

Thanks so much!