VInterpTo doesn't smoothly move camera

Hello everyone! I am really struggling with this and cannot figure out why for the life of me the VInterpTo isn’t smoothing my camera’s movement out. I’ve tried searching online to no luck. My code is below:

void ATheMeadowsCharacter::SwitchCamera()
{
	currentCameraPos = cameraTransformA;
	cameraSwitch = !cameraSwitch;

	if (cameraSwitch == false)
	{
		cameraTransformA.X = 20.0f;
		cameraTransformA.Y = 40.0f;
		cameraTransformA.Z = 40.0f;
	}
	else if (cameraSwitch == true)
	{
		cameraTransformA.X = 20.0f;
		cameraTransformA.Y = -40.0f;
		cameraTransformA.Z = 40.0f;
	}

	isSwitchedCamera = true;
}

void ATheMeadowsCharacter::Tick(float DeltaSeconds)
{
	Super::Tick(DeltaSeconds);

	if (isSwitchedCamera == true)
	{
		FVector finalCamPos = FMath::VInterpTo(currentCameraPos, cameraTransformA, GetWorld()->GetDeltaSeconds(), 0.5f);
		FollowCamera->SetRelativeLocation(finalCamPos);
		isSwitchedCamera = false;
	}
}

Everything compiles fine, but when I go in game and press the “SwitchCamera” key, my camera switches to the position but doesn’t smoothly do so; it teleports to it. I’m really confused as to why this does not work. If anybody can help me and describe the problem surrounding this, I will be forever grateful :slight_smile:

Thanks!

You’re only running your tick function once because you set IsSwitchedCamera to false the first time you move the camera instead of all the time.

edit:
Adding this here after much discussion below.

The deltatime wasn’t being accumulated anywhere, so it was just always calling VInterpTo with a delta of some really small number around 0.01 so it didn’t look like it was moving.

float _accumulatorFloat; 
 
 void ATheMeadowsCharacter::SwitchCamera()
  {
      currentCameraPos = cameraTransformA;
      cameraSwitch = !cameraSwitch;
  
      if (cameraSwitch == false)
      {
          cameraTransformA.X = 20.0f;
          cameraTransformA.Y = 40.0f;
          cameraTransformA.Z = 40.0f;
      }
      else if (cameraSwitch == true)
      {
          cameraTransformA.X = 20.0f;
          cameraTransformA.Y = -40.0f;
          cameraTransformA.Z = 40.0f;
      }
      _accumulatorFloat = 0.0f;
      isSwitchedCamera = true;
  }
  
  void ATheMeadowsCharacter::Tick(float DeltaSeconds)
  {
      Super::Tick(DeltaSeconds);
  
      if (isSwitchedCamera == true)
      {
          _accumulatorFloat += DeltaSeconds;
          FVector finalCamPos = FMath::VInterpTo(currentCameraPos, cameraTransformA, _accumulatorFloat , 0.5f);
          FollowCamera->SetRelativeLocation(finalCamPos);
          isSwitchedCamera = false;
      }
  }

@mrooney I’ve taken this out but it still seems to just jump straight to the position. I’m having a real hard time trying to figure out the problem!

Hrm. Try sticking this in there around the SetRelativeLocation.

GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, finalCamPos.ToString());

and seeing what pops out.

I get X=20.000 Y=38.333 Z=40.000 Then when I press the button again it goes straight to X=20.000 Y=-38.333 Z=40.000

I’m gunna try debugging some other things and I’ll update on my progress! Thanks :slight_smile:

Can you try it with passing in the DeltaSeconds instead of the GetWorld() function?

Yeah I’ve tried that, still with no results!

What kind of values do you have for your DeltaSeconds? Somewhere in the 0.04ish range or less?

Follow up question, what is currentCameraPos? Just an FVector or an FVector&?

If you put a breakpoint at line 19 above, are currentCameraPos and cameraTransformA different?

Sorry for the late reply! I can’t access my Visual Studio Debugger for some reason, I keep getting a Remote Debugging Monitor error. I’ll update you with the answers once I get it working

Right, so I am able to debug now. currentCameraPos is just an FVector and when the breakpoint is hit on line 19 they are both different values. May I ask how would I check the DeltaSeconds?

Just stick a breakpoint in the tick function or a print if that doesn’t work.

Maybe check in your tick that currentCameraPos and cameraTransformA are still different.

Yup all the values are different all the way through. In game the camera does change its position but it doesn’t smoothly move it to the position, it just teleports the camera to it!

@mrooney My DeltaSeconds states: 0.333

Do you know how to step into functions in the debugger? It’s like the little arrow that’s pointing at a dot in the debug buttons or f11. You can try stepping into VInterpTo.

The only things that should be causing it to jump all the way to the end are if your speed is 0, the distance it’s trying to travel is 0, or the time*speed >= 1, which I can’t see anywhere being the case with the code you’ve given :confused:

Thanks for your patience, I really appreciate it! So the Interp Speed = 0.0099 and the DeltaTime = 0.00833

So pretty much there is no speed. Do you know why this would be?

I do not, but the interp speed is bonkers because you pass it a literal float, and I am confused :frowning:

But even then, if you’re getting that it should almost not move at all.

Which config are you running in Debug/Debug Editor/Development/Development Editor?

I am running in the Development Editor.

Your debug symbols might just be wonky because of that. Try doing all the same tests in Debug Editor if you can. Sometimes variables get optimized away and display incorrectly in VS.

Yeah so when stepping into the function the Interp Speed is 0.5 with the DeltaTime being 0.008333

The currentCameraPos and cameraTransformA are different.