Search code examples
c++unreal-engine4unreal-development-kit

Changing FOV(Field of view) while moving forward


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?


Solution

  • 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);
        }