当前位置:网站首页>Basic character axis binding and mapping binding of u++ learning notes
Basic character axis binding and mapping binding of u++ learning notes
2022-07-23 21:45:00 【It's a bald rabbit】
PawnWithCamera.h:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "PawnWithCamera.generated.h"
UCLASS()
class FLOATINGCUBE_API APawnWithCamera : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
APawnWithCamera();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
UPROPERTY(EditAnywhere)
class USpringArmComponent* SpringArmComp;
UPROPERTY(EditAnywhere)
class UCameraComponent* CameraComp;
UPROPERTY(EditAnywhere)
class UStaticMeshComponent* StaticMeshComp;
FVector2D MovementInput;
FVector2D CameraInput;
float ZoomFactor;
bool bZoomingIn;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
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 "PawnWithCamera.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
#include "FloatingCube.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.
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootSceneComponent"));
StaticMeshComp = CreateDefaultSubobject <UStaticMeshComponent>(TEXT("StaticMeshComponent"));
SpringArmComp = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArmComponent"));
CameraComp = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComponent"));
//Attach our components
StaticMeshComp->SetupAttachment(RootComponent);
SpringArmComp->SetupAttachment(StaticMeshComp);
CameraComp->SetupAttachment(SpringArmComp, USpringArmComponent::SocketName);
//Assign SpringArm class variables.
SpringArmComp->SetRelativeLocationAndRotation(FVector(0.0f, 0.0f, 50.0f), FRotator(-60.0f, 0.0f, 0.0f));
SpringArmComp->TargetArmLength = 400.f;
SpringArmComp->bEnableCameraLag = true;
SpringArmComp->CameraLagSpeed = 3.0f;
//Take control of the default Player
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);
CameraComp->FieldOfView = FMath::Lerp<float>(90.0f, 60.0f, ZoomFactor);
SpringArmComp->TargetArmLength = FMath::Lerp<float>(400.0f, 300.0f, ZoomFactor);
{
FRotator NewRotation = GetActorRotation();
NewRotation.Yaw += CameraInput.X;
SetActorRotation(NewRotation);
}
{
FRotator NewRotation = SpringArmComp->GetComponentRotation();
NewRotation.Pitch = FMath::Clamp(NewRotation.Pitch + CameraInput.Y, -80.0f, -15.0f);
SpringArmComp->SetWorldRotation(NewRotation);
}
{
if (!MovementInput.IsZero())
{
//Scale our movement input axis values by 100 units per second
MovementInput = MovementInput.GetSafeNormal() * 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(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
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("CameraPitch", this, &APawnWithCamera::PitchCamera);
InputComponent->BindAxis("CameraYaw", this, &APawnWithCamera::YawCamera);
}
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;
}
边栏推荐
- A stack of digital robots were selected in Gartner's China AI market guide
- 存储结构和管理盘。有点像装win98要先分区格式化硬盘
- MySQL数据库索引
- Openlayers instance advanced view positioning advanced view positioning
- 集群聊天服務器:數據庫錶的設計
- HANA SQL 的Union和Union All
- Scala programming (elementary)
- 程序员成长第二十六篇:如何开好每日晨会?
- Cmake learning
- 面试快速复习(三):概率论与数理统计
猜你喜欢

【愚公系列】2022年06月 .NET架构班 084-微服务专题 Abp vNext微服务通信

Euclidean clustering (API) and its single tree segmentation

prime_ series_ level-1

CMake的学习

合宙ESP32C3硬件配置信息串口打印輸出

A stack of digital robots were selected in Gartner's China AI market guide

阿里onedate分层思想
![[Yugong series] June 2022.Net architecture class 084- micro service topic ABP vNext micro service communication](/img/29/b73edbdb2409f40c904d126f9185d1.png)
[Yugong series] June 2022.Net architecture class 084- micro service topic ABP vNext micro service communication

集群聊天服务器:工程目录的创建

集群聊天服务器为什么引入负载均衡器
随机推荐
Openlayers instances advanced mapbox vector tiles advanced mapbox vector maps
Cmake learning
NLP field history most complete must read classic papers classification, sorting and sharing (with Chinese analysis)
基于速度、复杂性等因素比较KernelSHAP和TreeSHAP
ApplicationContext introduction
A stack of digital robots were selected in Gartner's China AI market guide
SQL injection attack
机器学习习题——对率回归
软件体系结构期末复习六十题
(note) learning rate setting of optimizer Adam
1309_ Add GPIO flip on STM32F103 and schedule test with FreeRTOS
Construction and application progress of ten billion level knowledge map of meituan brain
138 - query case - knowledge points involved: foreach traversal & computed calculation attributes & V-for loop
Detailed explanation of cesium events (mouse events, camera events, keyboard events, scene trigger events)
C——文件
-2021 sorting and sharing of the latest required papers related to comparative learning
scala編程(初級)
Unity—3D数学-Vector3
prime_ series_ level-1
集群聊天服务器:Model数据层的框架设计和数据库代码的封装