UE4通过鼠标在pawn四周移动摄像头

前面的文章咱们可以在场景中移动Actor,但是无法移动观察Actor的角度和方位,现在我们要设置可以移动摄像头的方位和观察的角度。

  1. 首先我们在【项目设置】->【输入】添加两个鼠标输入:MousePitch和MouseYaw
    在这里插入图片描述
    在代码中,绑定这两个输入内容。
	PlayerInputComponent->BindAxis(TEXT("CameraPitch"), this, &AColliderPawn::cameraPitch);
	PlayerInputComponent->BindAxis(TEXT("CameraYaw"), this, &AColliderPawn::cameraYaw);

记录鼠标移动的数据

void AColliderPawn::cameraPitch(float value) {
	//UE_LOG(LogTemp, Warning, TEXT("cameraPitch: %f"), value);
	cameraInput.Y = value;
}

void AColliderPawn::cameraYaw(float value) {
	//UE_LOG(LogTemp, Warning, TEXT("cameraYaw: %f"), value);
	cameraInput.X = value;
}

然后再Tick函数中实现功能

void AColliderPawn::Tick(float DeltaTime) {
	Super::Tick(DeltaTime);

	//左右移动
	FRotator newRotation = GetActorRotation();
	newRotation.Yaw += cameraInput.X;
	SetActorRotation(newRotation);

	//通过摇臂移动方位
	FRotator armRotation = mySpringArm->GetComponentRotation();
	//armRotation.Pitch += cameraInput.Y;
	armRotation.Pitch = FMath::Clamp((double)(armRotation.Pitch += cameraInput.Y), (double)-80.0, double(-15.0));
	mySpringArm->SetWorldRotation(armRotation);
}

完整的代码:

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "ColliderPawn.generated.h"

UCLASS()
class FIRSTPROJECT_API AColliderPawn : public APawn
{
	GENERATED_BODY()

public:
	// Sets default values for this pawn's properties
	AColliderPawn();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	// Called to bind functionality to input
	virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;

public:

	//静态网格体
	UPROPERTY(VisibleAnywhere, Category = "MyMess")
	UStaticMeshComponent* myMess;
	FORCEINLINE void setMessComponent(UStaticMeshComponent* mess) {
		myMess = mess;
	}
	FORCEINLINE UStaticMeshComponent* getMessComponent() {
		return myMess;
	}

	//包围球
	UPROPERTY(VisibleAnywhere, Category = "MyMess")
	class USphereComponent* mySphereComponent;
	FORCEINLINE void setSphereComponent(USphereComponent* sphere) {
		mySphereComponent = sphere;
	}
	FORCEINLINE USphereComponent* getSphereComponent() {
		return mySphereComponent;
	}
	//摄像机
	UPROPERTY(VisibleAnywhere, Category = "MyMess")
	class UCameraComponent* myCamera;
	FORCEINLINE void setCameraComponent(UCameraComponent* camera) {
		myCamera = camera;
	}
	FORCEINLINE UCameraComponent* getCameraComponent() {
		return myCamera;
	}

	//摇臂
	UPROPERTY(VisibleAnywhere, Category = "MyMess")
	class USpringArmComponent* mySpringArm;
	FORCEINLINE void setSpringArmComponent(USpringArmComponent* arm) {
		mySpringArm = arm;
	}
	FORCEINLINE USpringArmComponent* getSpringArmComponent() {
		return mySpringArm;
	}

	//移动组件
	UPROPERTY(VisibleAnywhere, Category = "MyMess")
	class UColliderMovementComponent* myMovementComponent;
	virtual UPawnMovementComponent* GetMovementComponent()const override;

private:
	void moveForward(float value);
	void moveRight(float value);

	void cameraPitch(float value);
	void cameraYaw(float value);

private:
	FVector2D cameraInput;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "ColliderPawn.h"
#include "Components/SphereComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
#include "ColliderMovementComponent.h"

// Sets default values
AColliderPawn::AColliderPawn() {
	// 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;

	RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));

	mySphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("MySphere"));
	mySphereComponent->InitSphereRadius(40.0);
	mySphereComponent->SetCollisionProfileName(TEXT("Pawn"));
	mySphereComponent->SetupAttachment(GetRootComponent());

	myMess = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyMess"));
	myMess->SetupAttachment(GetRootComponent());

	//通过代码的方式设置静态网格体
	static ConstructorHelpers::FObjectFinder<UStaticMesh> messAsset(TEXT("StaticMesh'/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere'"));
	if (messAsset.Succeeded()){
		myMess->SetStaticMesh(messAsset.Object);
		myMess->SetRelativeLocation(FVector(0.0, 0.0, -40.0));
		myMess->SetWorldScale3D(FVector(0.8));
	}

	mySpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("MySpringArm"));
	mySpringArm->SetupAttachment(GetRootComponent());
	mySpringArm->SetRelativeRotation(FRotator(-45.0, 0.0, 0.0));
	mySpringArm->TargetArmLength = 400.0;
	mySpringArm->bEnableCameraLag = true;
	mySpringArm->CameraLagSpeed = 3.0;

	myCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("MyCamera"));
	myCamera->SetupAttachment(mySpringArm, USpringArmComponent::SocketName);

	myMovementComponent = CreateDefaultSubobject<UColliderMovementComponent>(TEXT("MyMovementCompoment"));
	myMovementComponent->UpdatedComponent = RootComponent;

	cameraInput = FVector2D(0.0);

	//自动支配
	AutoPossessPlayer = EAutoReceiveInput::Player0;
}

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

// Called every frame
void AColliderPawn::Tick(float DeltaTime) {
	Super::Tick(DeltaTime);

	//左右移动
	FRotator newRotation = GetActorRotation();
	newRotation.Yaw += cameraInput.X;
	SetActorRotation(newRotation);

	//通过摇臂移动方位
	FRotator armRotation = mySpringArm->GetComponentRotation();
	//armRotation.Pitch += cameraInput.Y;
	armRotation.Pitch = FMath::Clamp((double)(armRotation.Pitch += cameraInput.Y), (double)-80.0, double(-15.0));
	mySpringArm->SetWorldRotation(armRotation);
}

// Called to bind functionality to input
void AColliderPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) {
	Super::SetupPlayerInputComponent(PlayerInputComponent);

	PlayerInputComponent->BindAxis(TEXT("MoveForward"), this, &AColliderPawn::moveForward);
	PlayerInputComponent->BindAxis(TEXT("MoveRight"), this, &AColliderPawn::moveRight);

	PlayerInputComponent->BindAxis(TEXT("CameraPitch"), this, &AColliderPawn::cameraPitch);
	PlayerInputComponent->BindAxis(TEXT("CameraYaw"), this, &AColliderPawn::cameraYaw);
}

UPawnMovementComponent* AColliderPawn::GetMovementComponent()const {
	return myMovementComponent;
}

void AColliderPawn::moveForward(float value) {
	FVector forword = GetActorForwardVector();
	if (myMovementComponent){
		myMovementComponent->AddInputVector(forword * value);
	}
}

void AColliderPawn::moveRight(float value) {
	FVector right = GetActorRightVector();
	if (myMovementComponent) {
		myMovementComponent->AddInputVector(right * value);
	}
}

void AColliderPawn::cameraPitch(float value) {
	//UE_LOG(LogTemp, Warning, TEXT("cameraPitch: %f"), value);
	cameraInput.Y = value;
}

void AColliderPawn::cameraYaw(float value) {
	//UE_LOG(LogTemp, Warning, TEXT("cameraYaw: %f"), value);
	cameraInput.X = value;
}

aaa

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wb175208

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值