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;
}

编译之后,创建蓝图类
在这里插入图片描述
最后,我们可在运行时设置初始值看看效果

// 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);
}

在这里插入图片描述

鼠标控制移动

鼠标按键映射绑定

创建鼠标滑轮
在这里插入图片描述
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);
        }
    }
}

最后,在场景中运行
在这里插入图片描述

  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值