首页 > 解决方案 > 前进时改变 FOV(视野)

问题描述

我有情况。我想在前进的同时顺利地改变 FOV。在父类的相机设置中,我设置了默认值:

FollowCamera->FieldOfView = 90.0f;

然后在 MoveForward 函数中我这样设置

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

实际上它可以工作,所以当我向前移动时,FOV 更改为 140,但它的工作原理非常粗略,并且会立即发生。我想从90到140顺利完成。你能帮我吗?

标签: c++unreal-engine4unreal-development-kit

解决方案


FInterpTo 会为你做这个: https ://docs.unrealengine.com/en-US/API/Runtime/Core/Math/FMath/FInterpTo/index.html

每次调用“MoveForward”时,fov 将增加直到 140。要减慢/加快过渡,请减少/增加 InterpSpeed 值

使用您的代码:

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

推荐阅读