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);
网友评论