当前位置:   article > 正文

UE5 C++ 创建可缩放的相机_ue5 实现变焦相机

ue5 实现变焦相机

一.要将相机设置在Pawn类里

1.在MyPawn头文件里,加上摇臂和相机组件

  1. #include "GameFramework/SpringArmComponent.h"
  2. #include "Camera/CameraComponent.h"

2.在Pawm里声明SceneComponet,SpringArmComponent,CameraComponent组件指针

再声明一个移动缩放调用的函数

  1. public:
  2. UPROPERTY(VisibleAnywhere,BlueprintReadOnly,Category = "MySceneComponent")
  3. USceneComponent* MyRoot;
  4. UPROPERTY(VisibleAnywhere,BlueprintReadOnly,Category = "MySceneComponent")
  5. USpringArmComponent* MySpringArm;
  6. UPROPERTY(VisibleAnywhere,BlueprintReadOnly,Category = "MySceneComponent")
  7. UCameraComponent* MyCamera;
  8. //鼠标划轮移动镜头缩放
  9. void Zoom(bool Direction,float ZoomSpeed);

在Pawn里将的组件通过CreateDefaultSubobject<T>(TEXT("Name"))创造命名。

根组件赋值为MyRoot的物体组件。SetupAttackment来连接子组件。将MySpringArm的bDoCollisionTest = false来停止碰撞。

  1. AMyPawn::AMyPawn()
  2. {
  3. // Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.
  4. PrimaryActorTick.bCanEverTick = true;
  5. MyRoot = CreateDefaultSubobject<USceneComponent>(TEXT("MyRootComponent"));
  6. MySpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("MySpringArmComponent"));
  7. MyCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("MyCameraComponent"));
  8. RootComponent = MyRoot;
  9. MySpringArm->SetupAttachment(MyRoot);
  10. MyCamera->SetupAttachment(MySpringArm);
  11. MySpringArm->bDoCollisionTest = false; //让它无碰撞

3.设置好Pawn的Transform信息。

  1. FVector MyLocation = FVector(0,0,0);
  2. FRotator MyRotation = FRotator(-50,0,0);
  3. FVector MyScale = FVector(1,1,1);
  4. SetActorLocation(MyLocation);
  5. SetActorRotation(MyRotation);
  6. SetActorScale3D(MyScale);

4.在滑动函数里,将相机的伸缩臂的伸缩方向和速度的逻辑写好

  1. void AMyPawn::Zoom(bool Direction, float ZoomSpeed)
  2. {
  3. if (Direction) //1
  4. {
  5. if (MySpringArm->TargetArmLength >= 300 && MySpringArm->TargetArmLength < 5000) //如果摄像机摇臂在300 到 5000之间
  6. {
  7. MySpringArm->TargetArmLength += (ZoomSpeed * 2);
  8. GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, FString::Printf(TEXT("SpringArmLength is %f"), MySpringArm->TargetArmLength));
  9. }
  10. }
  11. else
  12. {
  13. if (MySpringArm->TargetArmLength > 300 && MySpringArm->TargetArmLength <= 5000)
  14. {
  15. MySpringArm->TargetArmLength -= (ZoomSpeed * 2);
  16. GEngine->AddOnScreenDebugMessage(-1,5.0f,FColor::Red,FString::Printf(TEXT("SpringArmLength is %f"),MySpringArm->TargetArmLength));
  17. }
  18. }
  19. }

5.在PlayerController.cpp头文件里包含Pawn。注意顺序(MyPlayerController.h要放在第一个)

  1. #include "MyPlayerController.h"
  2. #include "MyPawn.h"

在PlayerController.h里声明 绑定输入 和 其他功能函数

  1. virtual void SetupInputComponent();
  2. void WheelUpFunction();
  3. void WheelDownFunction();

设置输入绑定事件,由于要重写SetupInputComponent虚函数,首先要继承父类的Super::SetupInputComponent(); 这个在UE里要写。再加上额外添加的代理绑定功能。

通过绑定输入代理,调用PlayerController的新定义的函数

  1. void AMyPlayerController::SetupInputComponent()
  2. {
  3. Super::SetupInputComponent();
  4. InputComponent->BindAction("WheelUp",IE_Pressed,this,&AMyPlayerController::WheelUpFunction);
  5. InputComponent->BindAction("WheelDown",IE_Pressed,this,&AMyPlayerController::WheelDownFunction);
  6. }

新定义的函数中,通过GetPawn()拿到,GameMode里的Pawn,把它转换为AMyPawn。如果转换成功,在调用里面的MyPawn里的Zoom函数。

  1. void AMyPlayerController::WheelUpFunction()
  2. {
  3. if (GetPawn())
  4. {
  5. AMyPawn* MyCameraPawn = Cast<AMyPawn>(GetPawn());
  6. if (MyCameraPawn)
  7. {
  8. MyCameraPawn->Zoom(true, 10);
  9. }
  10. }
  11. }
  12. void AMyPlayerController::WheelDownFunction()
  13. {
  14. if (GetPawn())
  15. {
  16. AMyPawn* MyCameraPawn = Cast<AMyPawn>(GetPawn());
  17. if (MyCameraPawn)
  18. {
  19. MyCameraPawn->Zoom(false, 10);
  20. }
  21. }
  22. }

可以伸缩,并且打印SpringArm的长度

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/231716
推荐阅读