美文网首页
Unreal 摄像机切换

Unreal 摄像机切换

作者: Mr_约科 | 来源:发表于2019-09-26 10:41 被阅读0次

    1. 准备两个摄像机

    //外部摄像机
    UPROPERTY(VisibleAnywhere, Category = "CameraComponent")
    UCameraComponent* ExternalCamera;
    //内部摄像机
    UPROPERTY(VisibleAnywhere, Category = "CameraComponent")
    UCameraComponent* InternalCamera;
    ---------------------------------------------------------------------
    ExternalCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("ExternalCamera"));
    ExternalCamera->SetupAttachment(GetMesh());
    InternalCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("InternalCamera"));
    InternalCamera->SetupAttachment(GetMesh());
    

    2. 实现切换

    在转换时,通常会用到 USceneComponent组件

    //USceneComponent组件的位置
    FVector InternalCameraOrign;
    UPROPERTY(VisibleAnywhere, Category = "CameraComponent")
    USceneComponent* InternalCameraBase;
    ---------------------------------------------------------------------
    InternalCameraOrign = FVector(-16.0f, -23.0f, 23.0f);
    InternalCameraBase = CreateDefaultSubobject<USceneComponent>(TEXT("InternalCameraBase"));
    InternalCameraBase->SetupAttachment(GetMesh());
    InternalCameraBase->SetRelativeLocation(InternalCameraOrign);
    InternalCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("InternalCamera"));
    InternalCamera->SetupAttachment(InternalCameraBase);
    

    3. 设置“切换”绑定

    //内部摄像机是否被激活
    bool bInCarCameraActive;
    //摄像机切换响应函数
    void OnToggleCamera();
    ---------------------------------------------------------------------
    void AVehicle::OnToggleCamera()
    {
        if (bInCarCameraActive)
        {
            ExternalCamera->Activate();//激活
            InternalCamera->Deactivate();//失活
            bInCarCameraActive = false;
        }
        else
        {
            InternalCamera->Activate();//激活
            ExternalCamera->Deactivate();//失活
            bInCarCameraActive = true;
        }
    }
    

    切换绑定

    //参数说明:响应名字(与项目设置中自定义的输入名称相同)、什么操作、哪个对象、哪个函数响应
    PlayerInputComponent->BindAction("SwitchCamera", IE_Pressed, this, 
    &AVehicle::OnToggleCamera);
    

    相关文章

      网友评论

          本文标题:Unreal 摄像机切换

          本文链接:https://www.haomeiwen.com/subject/vbdwuctx.html