0

I have situation. I want smoothly change FOV while moving forward. in Camera settings in parent class I set default value:

FollowCamera->FieldOfView = 90.0f;

Then in MoveForward function I set it like this

void AStarMotoVehicle::MoveForward(float Axis)
{
    if ((Controller != NULL) && (Axis != 0.0f) && (bDead != true))
    { 
        //Angle of direction of camera on Yaw
    const FRotator Rotation = Controller->GetControlRotation();
    const FRotator YawRotation(0, Rotation.Yaw, 0);

    const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
    AddMovementInput(Direction, Axis);

    FollowCamera->FieldOfView = 140.0f;
    }

Actually it works, so when I move forward FOV changes to 140, but it works really roughly and happens instantly. I want to do it smoothly from 90 to 140. Can you help me with it?

1 Answers1

2

FInterpTo will make this for you : https://docs.unrealengine.com/en-US/API/Runtime/Core/Math/FMath/FInterpTo/index.html

Each times the "MoveForward" will be called, the fov will be increased until 140. To slow/speed the transition, decrease/increase the InterpSpeed value

With your code :

void AStarMotoVehicle::MoveForward(float Axis)
{
    if ((Controller != NULL) && (Axis != 0.0f) && (bDead != true))
    { 
        //Angle of direction of camera on Yaw
    const FRotator Rotation = Controller->GetControlRotation();
    const FRotator YawRotation(0, Rotation.Yaw, 0);

    const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
    AddMovementInput(Direction, Axis);

    // could check if World is valid
    UWorld *World = GetWorld();
    

    const float CurrentFOV = FollowCamera->FieldOfView;
    const float InterpSpeed = 2.0f
    FollowCamera->FieldOfView = FMath::FInterpTo(CurrentFOV, 
     140.0f, 
     World->GetTimeSeconds(),
     InterpSpeed);
    }
MatthieuL
  • 514
  • 1
  • 3
  • 7
  • Thank you for the answer. But when I launch it and after trying to move forward. Application crashes. And I suspect this is because I'm not getting GetTimeSeconds correctly. I created Uworld * World class(inside my vehicle) it seems it is not correct, right? – Fedor Petrenko Jul 08 '20 at 06:37
  • Yes, to get the World in an Actor : https://docs.unrealengine.com/en-US/API/Runtime/Engine/GameFramework/AActor/GetWorld/index.html I thought you know how to get the World, it's why I wrote directly World, I'll update my code. @FedorPetrenko done. – MatthieuL Jul 08 '20 at 06:53