UE5学习笔记-1-camera-spring-paperflipbook-inputmapping

文章发布时间:

最后更新时间:

页面浏览: 加载中...

UPROPERTY

UPROPERTY可以使变量在引擎中编辑。

1
2
3
4
5
6
7
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Camera,mata = (ClampMin = -5, ClampMax = 5))
int a;
// 这样就能在引擎中编辑a了。
//EditAnywhere任何位置可编辑,
//BlueprintReadWrite蓝图读写,
//Category = Camera分类为camera,
//mata = (ClampMin = -5, ClampMax = 5)编辑器中变量滑块,最小值为-5,最大5

UFUNCTION

UFUNCTION可以使函数可在蓝图编辑器里使用。

在MyActor里创建
在.h文件里声明函数

1
2
3
4

public:
int SetNum(int a);

点击改锥一键在.cpp文件里定义函数

1
2
3
4
5
6

int AMyActor::SetNum(int a)
{
return 0;
}

在.h文件中的声明之前加上UFUNCTION:

1
2
3
4
5
6
7


public:
UFUNCTION(BlueprintCallable,Category = "My" )
int SetNum(int a);


这样就能在蓝图中调用SetNum啦。 ^ ^

创建角色

创建控制角色–在ue引擎里创建C++,character类–MyCharacter。

创建基于MyCharacter的子蓝图,可以在蓝图里更改相关信息。(添加网格体)

打开C++文件

添加摄像机,摄像机臂

在.cpp文件里添加头文件

1
2
#include "Camera/CameraComponent.h"
#include "GameFramework/SpringArmComponent.h"

在.h文件里声明摄像机,摄像机臂

1
2
3
4
5
6
7
8
9
10
11
public:
// Sets default values for this character's properties
AMyCharacter();


//添加摄像机
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera)
class UCameraComponent* P_Camera;
//添加摄像机臂
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera)
class USpringArmComponent* CameraSpring;

在.cpp文件中加入相机臂和相机代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14

//设置摄像机臂
CameraSpring = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraSpring"));
CameraSpring->SetupAttachment(RootComponent);
CameraSpring->bDoCollisionTest = false;
CameraSpring->bUsePawnControlRotation = true;
//设置摄像机臂旋转



//设置摄像机
P_Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("P_Camera"));
P_Camera->SetupAttachment(CameraSpring);

其他设置
设置弹簧臂相对旋转不能使弹簧臂旋转,所以我旋转不设置弹簧臂长度,设置弹簧臂插座偏移

1
2
3
4
5
6
7
8
9

//设置一个摄像机或其他对象是否使用绝对旋转
CameraSpring->SetUsingAbsoluteRotation(true); // Don't want arm to rotate when character does
CameraSpring->TargetArmLength = 0.f;
//设置Springarm的相对旋转
CameraSpring->SetWorldRotation(FRotator(-90.f, 0.f, 0.f));
//设置spring插座偏移
CameraSpring->SocketOffset = FVector(0, 0, 600);

增强输入

移动

移动输入映射

头文件中声明
in public

1
2
3
4
5
6
7
8
9
10


//角色移动输入
UPROPERTY(EditAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"), Category = Input)
class UInputAction* IA_Move;

//输入映射
UPROPERTY(EditAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"), Category = Input)
class UInputMappingContext* InputMapping;

in protected

1
2
3

void Move(const FInputActionValue& Value);

in .cpp
void AMyCharacter::BeginPlay()添加,
或者改锥一键生成。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

// 增强输入:移动输入
void AMyCharacter::Move(const FInputActionValue& Value)
{
// input is a Vector2D
FVector2D MovementVector = Value.Get<FVector2D>();

if (Controller != nullptr)
{
// find out which way is forward
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);

// get forward vector
const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);

// get right vector
const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);

// add movement
AddMovementInput(ForwardDirection, MovementVector.Y);
AddMovementInput(RightDirection, MovementVector.X);
}

}

增强输入绑定

void AMyCharacter::Tick(float DeltaTime)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

// Called to bind functionality to input
// 增强输入的绑定
void AMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);


// Add Input Mapping Context
if (APlayerController* PlayerController = Cast<APlayerController>(GetController()))
{
if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer()))
{
Subsystem->AddMappingContext(InputMapping, 0);
}
}

// Set up action bindings
if (UEnhancedInputComponent* EnhancedInputComponent = Cast<UEnhancedInputComponent>(PlayerInputComponent)) {

// Jumping
//EnhancedInputComponent->BindAction(IA_Jump, ETriggerEvent::Started, this, &AMyCharacter::Jump);
//EnhancedInputComponent->BindAction(IA_Jump, ETriggerEvent::Completed, this, &AMyCharacter::StopJumping);


// Moving
EnhancedInputComponent->BindAction(IA_Move, ETriggerEvent::Triggered, this, &AMyCharacter::Move);
//ETriggerEvent::Triggered 持续触发 Started 单次触发

// Looking
//EnhancedInputComponent->BindAction(IA_Look, ETriggerEvent::Triggered, this, &ATgameCharacter::Look);
}
else
{
//错误日志
//UE_LOG(LogTemplateCharacter, Error, TEXT("'%s' Failed to find an Enhanced Input component! This template is built to use the Enhanced Input system. If you intend to use the legacy system, then you will need to update this C++ file."), *GetNameSafe(this));
}
}

最后在引擎中建立蓝图IA_move,IMC_Default,编辑好输入映射。再在角色蓝图中将变量IA_Move,InputMapping赋值为上面两个蓝图,角色就可以移动了。

视角

角色旋转

.h
public

1
2
3
4
5

//角色旋转
UPROPERTY(EditAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"), Category = Input)
class UInputAction* IA_Look;

protected:

1
2
3

void Look(const FInputActionValue& Value);

改锥一键生成
in .cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14

void AMyCharacter::Look(const FInputActionValue& Value)
{
// input is a vector2D
FVector2D LookAxisVector = Value.Get<FVector2D>();

if(Controller != nullptr)
{
// add yaw and pitch input to controller
AddControllerYawInput(LookAxisVector.X);
AddControllerPitchInput(LookAxisVector.Y);
}
}

增强输入修改

修改增强输入绑定部分。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

// Called to bind functionality to input
// 增强输入的绑定
void AMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);


// Add Input Mapping Context
if (APlayerController* PlayerController = Cast<APlayerController>(GetController()))
{
if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer()))
{
Subsystem->AddMappingContext(InputMapping, 0);
}
}

// Set up action bindings
if (UEnhancedInputComponent* EnhancedInputComponent = Cast<UEnhancedInputComponent>(PlayerInputComponent)) {

// Jumping
//EnhancedInputComponent->BindAction(IA_Jump, ETriggerEvent::Started, this, &AMyCharacter::Jump);
//EnhancedInputComponent->BindAction(IA_Jump, ETriggerEvent::Completed, this, &AMyCharacter::StopJumping);

// Moving
EnhancedInputComponent->BindAction(IA_Move, ETriggerEvent::Triggered, this, &AMyCharacter::Move);
//ETriggerEvent::Triggered 持续触发 Started 单次触发

// Looking
EnhancedInputComponent->BindAction(IA_Look, ETriggerEvent::Triggered, this, &AMyCharacter::Look);
}
else
{
//错误日志
//UE_LOG(LogTemplateCharacter, Error, TEXT("'%s' Failed to find an Enhanced Input component! This template is built to use the Enhanced Input system. If you intend to use the legacy system, then you will need to update this C++ file."), *GetNameSafe(this));
}
}


最后在角色蓝图中绑定IA_Look.

视角限制

在cpp,beginplay中设置.
通过方法ViewPitchMax/Min.

1
2
3
4
5
6
7


//设置俯仰视角范围
APlayerController* PlayerControl = Cast<APlayerController>(Controller);
PlayerControl->PlayerCameraManager->ViewPitchMax = 45.0f;
PlayerControl->PlayerCameraManager->ViewPitchMin = -45.0f;

角色跳跃

.h

1
2
3
//角色跳跃
UPROPERTY(EditAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"), Category = Input)
class UInputAction* IA_Jump;

修改增强输入绑定

.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Called to bind functionality to input
// 增强输入的绑定
void AMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);


// Add Input Mapping Context
if (APlayerController* PlayerController = Cast<APlayerController>(GetController()))
{
if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer()))
{
Subsystem->AddMappingContext(InputMapping, 0);
}
}

// Set up action bindings
if (UEnhancedInputComponent* EnhancedInputComponent = Cast<UEnhancedInputComponent>(PlayerInputComponent)) {

// Jumping
EnhancedInputComponent->BindAction(IA_Jump, ETriggerEvent::Started, this, &AMyCharacter::Jump);
EnhancedInputComponent->BindAction(IA_Jump, ETriggerEvent::Completed, this, &AMyCharacter::StopJumping);

// Moving
EnhancedInputComponent->BindAction(IA_Move, ETriggerEvent::Triggered, this, &AMyCharacter::Move);
//ETriggerEvent::Triggered 持续触发 Started 单次触发

// Looking
EnhancedInputComponent->BindAction(IA_Look, ETriggerEvent::Triggered, this, &AMyCharacter::Look);
}
else
{
//错误日志
//UE_LOG(LogTemplateCharacter, Error, TEXT("'%s' Failed to find an Enhanced Input component! This template is built to use the Enhanced Input system. If you intend to use the legacy system, then you will need to update this C++ file."), *GetNameSafe(this));
}
}

这里直接用了引擎自带的封装好的jump函数,没有覆写。

限制跳跃高度

.h
MyCaracter

1
2
// 设置跳跃速度
GetCharacterMovement()->JumpZVelocity = 300.f;

最后在角色蓝图中设置IA_Jump就ok了。

设置2DFlipbook

在.build.cs文件中插入PaperZD插件,我用了Resharper直接生成,应该有别的更改。会生成一个PaperZDCharacter.h文件。

1
2
3

PrivateDependencyModuleNames.AddRange(new string[] { "Paper2D" });

这样就能使用sprite和flipbook了。

在.h文件里

1
2
3
4
5

//精灵
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Character, meta = (AllowPrivateAccess = "true"))
class UPaperFlipbookComponent* P_Flipbook;

在cpp文件里

1
2
3

P_Flipbook = CreateDefaultSubobject<UPaperFlipbookComponent>(TEXT("P_Flipbook"));

默认生成在胶囊体的子组件,如果不是,重新在头文件里声明一个胶囊体,再用SetUpAttachment()函数解决。