当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

壹沓数字机器人入选Gartner《中国AI市场指南》

2022-7-23 12点 程序爱生活 小时线顶背离出现,保持下跌趋势,等待反弹信号出现。

The total ranking of blogs is 918

Cluster chat server: Design of database table

Cluster chat server: creation of project directory

LeetCode_ 376_ Wobble sequence

集群聊天服务器:数据库表的设计

-2021 sorting and sharing of the latest required papers related to comparative learning

VLAN comprehensive experiment

数据库系统概论第五版课后习题——第一章 绪论
随机推荐
Distributed transaction scheme: best effort notification scheme
Postgraduate entrance examination | advanced mathematics Chapter4 indefinite integral
集群聊天服务器:工程目录的创建
HANA SQL 的Union和Union All
MySQL数据库索引
query中的customer exit客户出口变量
Euclidean clustering (API) and its single tree segmentation
googletest
寻找消失的类名
Day109. Shangyitong: integrate Nacos, hospital list, drop-down list query, hospital online function, hospital details query
How to use cesium knockout?
2022.7.22 JS object
剑指 Offer II 115. 重建序列 : 拓扑排序构造题
Summary of database stress testing methods
Protocol buffers 的问题和滥用
TreeMap
Day109.尚医通:集成Nacos、医院列表、下拉列表查询、医院上线功能、医院详情查询
& 9 nodemon automatic restart tool
数据库压力测试方法小结
Cmake learning