UE5 C++(十)— 鼠标控制相机移动

文章目录

  • 创建摄像机摇臂
  • 鼠标控制移动
    • 鼠标按键映射绑定
    • 控制镜头缩放

创建摄像机摇臂

在Default Pawn Class 继承类MyPawn中实现
首先添加摇臂和相机组件

public:
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MySceneComponent")
	USceneComponent *MyRootComponent;
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MySceneComponent")
	USpringArmComponent *MySpringArmComponent;
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MySceneComponent")
	UCameraComponent *MyCameraComponent;

然后,在构造函数中实现

// Sets default values
AMyPawn::AMyPawn()
{
	// Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
	// 创建相机及摇臂
	MyRootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("MySceneComponent"));
	RootComponent = MyRootComponent;
	MySpringArmComponent = CreateDefaultSubobject<USpringArmComponent>(TEXT("MySpringArmComponent"));
	MySpringArmComponent->SetupAttachment(RootComponent);
	MyCameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("MyCameraComponent"));
	MyCameraComponent->SetupAttachment(MySpringArmComponent);
	MySpringArmComponent->bDoCollisionTest = false;
}

编译之后,创建蓝图类
UE5 C++(十)— 鼠标控制相机移动_第1张图片
最后,我们可在运行时设置初始值看看效果

// Called when the game starts or when spawned
void AMyPawn::BeginPlay()
{
	Super::BeginPlay();

	FVector MyLocation = FVector(0, 0, 245);
	FRotator MyRotation = FRotator(0, 0, 0);
	FVector MyScale = FVector(1, 1, 1);
	SetActorLocation(MyLocation);
	SetActorRotation(MyRotation);
	SetActorScale3D(MyScale);
}

UE5 C++(十)— 鼠标控制相机移动_第2张图片

鼠标控制移动

鼠标按键映射绑定

创建鼠标滑轮
UE5 C++(十)— 鼠标控制相机移动_第3张图片
Wheel_Up:代表鼠标滑轮往前移
Wheel_Down:代表鼠标滑轮后前移

控制镜头缩放

在默认Pawn中添加相机对外控制方法

MyPawn.h

public:
	// 鼠标滑轮移动、旋转、缩放镜头
	void Zoom(bool Direction, float ZoomSpeed);

MyPawn.cpp

void AMyPawn::Zoom(bool Direction, float ZoomSpeed)
{
	if (Direction)
	{
		MySpringArmComponent->TargetArmLength += ZoomSpeed * 2;
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("TargetArmLength is %f"), MySpringArmComponent->TargetArmLength));
	}
	else
	{
		MySpringArmComponent->TargetArmLength -= ZoomSpeed * 2;
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("TargetArmLength is %f"), MySpringArmComponent->TargetArmLength));
	}
}

之后,
添加AMyPlayerController.h中添加输入绑定

public:
	// Allows the PlayerController to set up custom input bindings.
	virtual void SetupInputComponent();
	void WheelUpFunction();
	void WheelDownFunction();

添加AMyPlayerController.cpp中实现绑定

#include "MyPawn.h"
#include "MyPlayerController.h"

void AMyPlayerController::SetupInputComponent()
{
    Super::SetupInputComponent();
    InputComponent->BindAction("Wheel_Up", IE_Pressed, this, &AMyPlayerController::WheelUpFunction);
    InputComponent->BindAction("Wheel_Down", IE_Released, this, &AMyPlayerController::WheelDownFunction);
}
void AMyPlayerController::WheelUpFunction()
{
//获取默认的Pawn
    if (GetPawn())
    {
        // GetPawn()->AddActorLocalOffset(FVector(0, 0, 10));
        AMyPawn *MyCameraPawn = Cast<AMyPawn>(GetPawn());
        if (MyCameraPawn)
        {
            MyCameraPawn->Zoom(1, 10);
        }
    }
}
void AMyPlayerController::WheelDownFunction()
{
    if (GetPawn())
    {
        AMyPawn *MyCameraPawn = Cast<AMyPawn>(GetPawn());
        if (MyCameraPawn)
        {
            MyCameraPawn->Zoom(0, 10);
        }
    }
}

最后,在场景中运行

你可能感兴趣的:(UE5,C++,入门开发,ue5,c++,开发语言)