UE C++ 多镜头设置缩放 平移

时间:2024-07-08 15:31:41

一.整体思路

首先需要在 想要控制的躯体Pawn上,生成不同相机对应的SpringArm组件。其次是在Controller上,拿到这个Pawn,并在其中设置输入响应,并定义响应事件。响应事件里有指向Pawn的指针,并把Pawn的缩放平移功能进行调用。

二.

1.CameraPawn的组件构成

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = MySceneComponent)
	USceneComponent* MyRoot;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = MySceneComponent)
	USpringArmComponent* MySpringArm;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = MySceneComponent)
	UCameraComponent* MyCamera;

	void Zoom(bool Direction, float ZoomSpeed);
	void Panx(float x);
	void Pany(float y);
	void Back();
ACameraPawn::ACameraPawn()
{
 	// 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;
	MyRoot = CreateDefaultSubobject<USceneComponent>(TEXT("MyRootComponent"));
	MySpringArm = CreateDefaultSubobject <USpringArmComponent>(TEXT("MySpringArmComponent"));
	MyCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("MySCameraComponent"));
	RootComponent = MyRoot;
	MySpringArm->SetupAttachment(MyRoot);
	MyCamera->SetupAttachment(MySpringArm);
	MySpringArm->bDoCollisionTest = false;
}

2.将Pawn自己镜头伸缩,平移功能,复原写好

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

void ACameraPawn::Panx(float x)
{
	MyCamera->AddLocalOffset(FVector(0,x,0));
}

void ACameraPawn::Pany(float y)
{
	MyCamera->AddLocalOffset(FVector(0,0,y));
}

void ACameraPawn::Back()
{
	MyCamera->SetRelativeLocation(FVector(0,0,0));
}

 3.在MyPlayerController里面绑定输入映射,以及响应的事件。

	virtual void SetupInputComponent();

	void WheelUpFunction();

	void WheelDownFunction();

	void PanX(float x);
	void PanY(float y);
	void Back();
	

4.绑定映射

void AMyPlayerController::SetupInputComponent()
{
	Super::SetupInputComponent();
	InputComponent->BindAction("WheelUp", IE_Pressed, this, &AMyPlayerController::WheelUpFunction);
	InputComponent->BindAction("WheelDown", IE_Pressed, this, &AMyPlayerController::WheelDownFunction);
	InputComponent->BindAction("BackXY",IE_Pressed,this,&AMyPlayerController::Back);
	InputComponent->BindAxis("PanX",this,&AMyPlayerController::PanX);
	InputComponent->BindAxis("PanY",this,&AMyPlayerController::PanY);
	//InputComponent->BindAction("ESCEvent", IE_Pressed, this, &AMyPlayerController::ESCEvent).bExecuteWhenPaused=true;//游戏暂停可以执行
	//InpuptComponent->BindAxis("PanY",this,);
}

5.响应事件,并操控控制到对应的Pawn里的函数。

void AMyPlayerController::WheelUpFunction()
{
	if (GetPawn()) {
		     ACameraPawn* MyCameraPawn = Cast<ACameraPawn>(GetPawn());
				if (MyCameraPawn) {
					MyCameraPawn->Zoom(1, 10);
				}
			}
}

void AMyPlayerController::WheelDownFunction()
{
		if (GetPawn()) {
			ACameraPawn* MyCameraPawn = Cast<ACameraPawn>(GetPawn());
		if (MyCameraPawn) {
			MyCameraPawn->Zoom(0, 10);
		}
	}
}

void AMyPlayerController::PanX(float x)
{
	if (GetPawn())
	{
		ACameraPawn* MyCameraPawn = Cast<ACameraPawn>(GetPawn());
		if (MyCameraPawn) {
			MyCameraPawn->Panx(x);
		}
	}
}

void AMyPlayerController::PanY(float y)
{
	if (GetPawn())
	{
		ACameraPawn* MyCameraPawn = Cast<ACameraPawn>(GetPawn());
		if (MyCameraPawn) {
			MyCameraPawn->Pany(y);
		}
	}
}

void AMyPlayerController::Back()
{
	if (GetPawn())
	{
		ACameraPawn* MyCameraPawn = Cast<ACameraPawn>(GetPawn());
		if (MyCameraPawn) {
			MyCameraPawn->Back();
		}
	}
}

6.需要在ProjectSetting里面,设置使用的按键。

三.可能需要注意的

1.Pawn这里,需要Poccess。不然会控制不到。

2.SpringArm设置视角的位置。Camera最好都是相对位置为0,这样视角返回默认值可以用。

MyCamera->SetRelativeLocation(FVector(0,0,0));