UE4学习笔记13th:使用C++ 代码以对输入进行响应

这节对输入进行响应。
首先打开PawnWithCamera.h添加定义:

 //输入变量
FVector2D MovementInput;
FVector2D CameraInput;
float ZoomFactor;
bool bZoomingIn;

紧接着创建函数来追溯输入:

//输入函数
void MoveForward(float AxisValue);
void MoveRinht(float AxisValue);
void PitchCamera(float AxisValue);
void YawCamera(float AxisValue);
void ZoomIn();
void ZoomOut();

这里写图片描述

在PawnWithCamera.cpp中添加代码:

//处理输入
void APawnWithCamera::MoveForward(float AxisValue)
{
MovementInput.X = FMath::Clamp<float>(AxisValue, -1.0f, 1.0f);
}

void APawnWithCamera::MoveRight(float AxisValue)
{
MovementInput.Y = FMath::Clamp<float>(AxisValue, -1.0f, 1.0f);
}

void APawnWithCamera::PitchCamera(float AxisValue)
{
CameraInput.Y = AxisValue;
}

void APawnWithCamera::YawCamera(float AxisValue)
{
CameraInput.X = AxisValue;
}

void APawnWithCamera::ZoomIn()
{
bZoomingIn = true;
}

void APawnWithCamera::ZoomOut()
{
bZoomingIn = false;
}

这里写图片描述

绑定到ApawnWithCamera::SetupPlayerInputComponent

//绑定到事件:“ZoomIn”
InputComponent->BindAction("ZoomIn", IE_Pressed, this, &APawnWithCamera::ZoomIn);
InputComponent->BindAction("ZoomIn", IE_Released, this, &APawnWithCamera::ZoomOut);//

//为四条轴绑定对每帧的处理
InputComponent->BindAxis("MoveForward", this, &APawnWithCamera::MoveForward);
InputComponent->BindAxis("MoveRight", this, &APawnWithCamera::MoveRight);
InputComponent->BindAxis("CameraYaw", this, &APawnWithCamera::YawCamera);
InputComponent->BindAxis("CameraPitch", this, &APawnWithCamera::PitchCamera);//

这里写图片描述

在APawnWithCamera::Tick中添加:用于更新每一帧的Pawn和Camera

//按下放大,否则缩小
{
if (bZoomingIn)
{
ZoomFactor += DeltaTime / 0.5f;//放大一半
}
else
{
ZoomFactor -= DeltaTime / 0.25f;//缩小四分之一
}
ZoomFactor = FMath::Clamp<float>(ZoomFactor, 0.0f, 1.0f);
//基于ZoomFActor来混合相机的视域和弹簧臂的长度
OurCamera->FieldOfView = FMath::Lerp<float>(90.0f, 60.0f, ZoomFactor);
OurCameraSpringArm->TargetArmLength = FMath::Lerp<float>(400.0f, 300.0f, ZoomFactor);
}

这里写图片描述

这个代码段会直接使用鼠标的X轴来旋转 Pawn 的偏转,但只有相机会对Y轴的变更进行响应, 旋转任意 Actor或 Actor 子类实际上会旋转根级Component ,而这又会间接影响所有附着的内容。

//旋转Actor的偏转,同时旋转附着在Actor上的相机
{
FRotator NewRotation = GetActorRotation();
NewRotation.Yaw += CameraInput.X;
SetActorRotation(NewRotation);
}//

//旋转相机的倾斜,使总是向下看
{
FRotator NewRotation = OurCameraSpringArm->GetComponentRotation();
NewRotation.Pitch = FMath::Clamp(NewRotation.Pitch + CameraInput.Y, -80.0f, -15.0f);
OurCameraSpringArm->SetWorldRotation(NewRotation);
}

这里写图片描述
处理移动:

//基于“MoveX”和“MoveY”坐标轴来处理移动
{
if (!MovementInput.IsZero())
{
//把移动输入坐标轴的值每秒缩放100个单位
MovementInput = MovementInput.SafeNormal() * 100.0f;//
FVector NewLocation = GetActorLocation();
NewLocation += GetActorForwardVector() * MovementInput.X *DeltaTime;
NewLocation += GetActorRightVector() * MovementInput.Y *DeltaTime;
SetActorLocation(NewLocation);
}
}

这里写图片描述

拖曳新类的一个实例到编辑器中的关卡编辑器窗口
这里写图片描述

按下Play,这是鼠标点击后的效果,你可以使用WASD控制移动,使用鼠标控制方向和缩放。

这里写图片描述

完整代码:

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

#pragma once

#include "GameFramework/Pawn.h"
#include "PawnWithCamera.generated.h"

UCLASS()
class HOWTO_PLAYERCAMERA_API APawnWithCamera : public APawn
{
GENERATED_BODY()

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

// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Called every frame
virtual void Tick( float DeltaSeconds ) override;

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

protected:
UPROPERTY(EditAnywhere)
USpringArmComponent* OurCameraSpringArm;
UCameraComponent* OurCamera;
//输入变量
FVector2D MovementInput;
FVector2D CameraInput;
float ZoomFactor;
bool bZoomingIn;//

//输入函数
void MoveForward(float AxisValue);
void MoveRight(float AxisValue);
void PitchCamera(float AxisValue);
void YawCamera(float AxisValue);
void ZoomIn();
void ZoomOut();//
};




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

#include "HowTo_PlayerCamera.h"
#include "PawnWithCamera.h"


// Sets default values
APawnWithCamera::APawnWithCamera()
{
 // 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("RootComponent"));
OurCameraSpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraSpringArm"));
OurCameraSpringArm->AttachTo(RootComponent);
OurCameraSpringArm->SetRelativeLocationAndRotation(FVector(0.0f, 0.0f, 50.0f), FRotator(-60.0f, 0.0f, 0.0f));
OurCameraSpringArm->TargetArmLength = 400.0f;
OurCameraSpringArm->bEnableCameraLag = true;
OurCameraSpringArm->CameraLagSpeed = 3.0f;//

//创建摄像机并附加到SpringArm组件上
OurCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("GameCamera"));
OurCamera->AttachTo(OurCameraSpringArm, USpringArmComponent::SocketName);//

//控制默认玩家
AutoPossessPlayer = EAutoReceiveInput::Player0;//


}

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

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

//按下放大,否则缩小
{
if (bZoomingIn)
{
ZoomFactor += DeltaTime / 0.5f;//放大一半
}
else
{
ZoomFactor -= DeltaTime / 0.25f;//缩小四分之一
}
ZoomFactor = FMath::Clamp<float>(ZoomFactor, 0.0f, 1.0f);
//基于ZoomFActor来混合相机的视域和弹簧臂的长度
OurCamera->FieldOfView = FMath::Lerp<float>(90.0f, 60.0f, ZoomFactor);
OurCameraSpringArm->TargetArmLength = FMath::Lerp<float>(400.0f, 300.0f, ZoomFactor);//
}//

//旋转Actor的偏转,同时旋转附着在Actor上的相机
{
FRotator NewRotation = GetActorRotation();
NewRotation.Yaw += CameraInput.X;
SetActorRotation(NewRotation);
}//

//旋转相机的倾斜,使总是向下看
{
FRotator NewRotation = OurCameraSpringArm->GetComponentRotation();
NewRotation.Pitch = FMath::Clamp(NewRotation.Pitch + CameraInput.Y, -80.0f, -15.0f);
OurCameraSpringArm->SetWorldRotation(NewRotation);
}//

//基于“MoveX”和“MoveY”坐标轴来处理移动
{
if (!MovementInput.IsZero())
{
//把移动输入坐标轴的值每秒缩放100个单位
MovementInput = MovementInput.SafeNormal() * 100.0f;//
FVector NewLocation = GetActorLocation();
NewLocation += GetActorForwardVector() * MovementInput.X *DeltaTime;
NewLocation += GetActorRightVector() * MovementInput.Y *DeltaTime;
SetActorLocation(NewLocation);
}
}//
}

// Called to bind functionality to input
void APawnWithCamera::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
Super::SetupPlayerInputComponent(InputComponent);

//绑定到事件:“ZoomIn”
InputComponent->BindAction("ZoomIn", IE_Pressed, this, &APawnWithCamera::ZoomIn);
InputComponent->BindAction("ZoomIn", IE_Released, this, &APawnWithCamera::ZoomOut);//

//为四条轴绑定对每帧的处理
InputComponent->BindAxis("MoveForward", this, &APawnWithCamera::MoveForward);
InputComponent->BindAxis("MoveRight", this, &APawnWithCamera::MoveRight);
InputComponent->BindAxis("CameraYaw", this, &APawnWithCamera::YawCamera);
InputComponent->BindAxis("CameraPitch", this, &APawnWithCamera::PitchCamera);//

}

//处理输入
void APawnWithCamera::MoveForward(float AxisValue)
{
MovementInput.X = FMath::Clamp<float>(AxisValue, -1.0f, 1.0f);
}

void APawnWithCamera::MoveRight(float AxisValue)
{
MovementInput.Y = FMath::Clamp<float>(AxisValue, -1.0f, 1.0f);
}

void APawnWithCamera::PitchCamera(float AxisValue)
{
CameraInput.Y = AxisValue;
}

void APawnWithCamera::YawCamera(float AxisValue)
{
CameraInput.X = AxisValue;
}

void APawnWithCamera::ZoomIn()
{
bZoomingIn = true;
}

void APawnWithCamera::ZoomOut()
{
bZoomingIn = false;
}//

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值