F16.SetViewTargetWithBlend|相机平滑切换
大约 2 分钟
方案1
- 定义一个接口和一个相机数组
//用于存场景中的相机
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Input, meta = (AllowPrivateAccess = "true"))
TArray<ACameraActor*> CameraArr;
UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category = "CameraInterface")
void IChangeCamera (int32 CameraID,float CameraSwitchTime) ;
- 继承并实现
//InterfaceChangeCamera Part1 .h 相机切换函数
void IChangeCamera_Implementation(int32 CameraID, float CameraSwitchTime) override;
void AInteractiveControllerPawn::IChangeCamera_Implementation(int32 CameraID, float CameraSwitchTime)
{
if (PlayerController)
{
UExorcistFunctionLibrary::SwitchCameraByID(PlayerController, CameraArr, CameraID, CameraSwitchTime);
GEngine->AddOnScreenDebugMessage(1, 1.0f, FColor::White, FString::SanitizeFloat(CameraID), true,
FVector2D(1, 1));
}
}
//可以将其放入蓝图函数库中
void UExorcistFunctionLibrary::SwitchCameraByID(APlayerController* PlayerController,
const TArray<ACameraActor*>& CameraArr, int32 CameraID,
float CameraSwitchTime /*= 2.0f*/)
{
// 检查输入的有效性
if (!PlayerController || CameraArr.Num() == 0 || CameraID < 0 || CameraID >= CameraArr.Num())
{
// 输入无效,提前返回
return;
}
// 获取当前相机
AActor* CurrentCamera = PlayerController->GetViewTarget();
// 检查相机ID是否与当前相机的ID相同
if (CameraArr[CameraID] == CurrentCamera)
{
// 相机已经处于活动状态,提前返回
return;
}
// 尝试将选择的相机角色转换为CameraActor类型
ACameraActor* SelectedCamera = Cast<ACameraActor>(CameraArr[CameraID]);
if (SelectedCamera)
{
// 将新相机设置为视角目标,并应用混合过渡效果
PlayerController->SetViewTargetWithBlend(SelectedCamera, CameraSwitchTime); // 可根据需要调整混合时间
}
}