摄像机应该是使用最普遍的组件了
获取摄像机,摄像机切换
-
新建C++类(以CameraActor为父类)
-
将摄像机在地图中放置
-
头文件声明
virtual void BeginPlay() override;UPROPERTY(EditAnywhere, BlueprintReadWrite)UBoxComponent* OverlapVolume; // 盒体组件,用于检测人物碰撞UPROPERTY(EditAnywhere, BlueprintReadWrite)TSubclassOf<ACameraActor> CameraToFind; //待寻找的摄像机UPROPERTY(EditAnywhere, BlueprintReadWrite)float CameraBlendTime; // 切换时间virtual void NotifyActorBeginOverlap(AActor* OtherActor) override;virtual void NotifyActorEndOverlap(AActor* OtherActor) override;
-
实现
void ABlendTriggerVolume::NotifyActorBeginOverlap(AActor* OtherActor) {Super::NotifyActorBeginOverlap(OtherActor);//如果是人物转换就能成功,反之nullptrif(AStaticCameraCharacter* PlayerCharacterCheck = Cast<AStaticCameraCharacter>(OtherActor)){//得到玩家角色的玩家控制器,同上if(APlayerController* PlayerCharacterController = Cast<APlayerController>(PlayerCharacterCheck->GetController())){TArray<AActor*> FoundActors;//获取所有对象的数组UGameplayStatics::GetAllActorsOfClass(GetWorld(), CameraToFind, FoundActors);//摄像机绑定PlayerCharacterController->SetViewTargetWithBlend(FoundActors[0], CameraBlendTime, EViewTargetBlendFunction::VTBlend_Linear);}} }
void ABlendTriggerVolume::NotifyActorEndOverlap(AActor* OtherActor) {Super::NotifyActorEndOverlap(OtherActor);if (AStaticCameraCharacter* PlayerCharacterCheck = Cast<AStaticCameraCharacter>(OtherActor)){if(APlayerController* PlayerCharacterController = Cast<APlayerController>(PlayerCharacterCheck->GetController())){PlayerCharacterController->SetViewTargetWithBlend(PlayerCharacterController->GetPawn(), CameraBlendTime, EViewTargetBlendFunction::VTBlend_Linear);}} }
-
之后我们基于摄像机类创建蓝图类,并设置相关内容
在蓝图细节中找到如下,并设置