创建一个pwan类型的类

1.打开 CollidingPawn.h 添加粒子组件
- UPROPERTY()
- class UParticleSystemComponent* OurParticleSystem;
2.打开cpp文件,首先引用要用到的头文件
- #include "UObject/ConstructorHelpers.h"
- #include "Particles/ParticleSystemComponent.h"
- #include "Components/SphereComponent.h"
- #include "Camera/CameraComponent.h"
- #include "GameFramework/SpringArmComponent.h"
3.在构造函数中添加各种组件和控制权
- // 根组件将成为对物理反应的球体
- USphereComponent* SphereComponent = CreateDefaultSubobject
(TEXT("RootComponent")); - RootComponent = SphereComponent;
- SphereComponent->InitSphereRadius(40.0f);
- //设置碰撞预设
- SphereComponent->SetCollisionProfileName(TEXT("Pawn"));
-
- // 创建并放置网格体组件,以便查看球体位置
- UStaticMeshComponent* SphereVisual = CreateDefaultSubobject
(TEXT("VisualRepresentation")); - SphereVisual->SetupAttachment(RootComponent);
- //从磁盘加载资源
- static ConstructorHelpers::FObjectFinder
SphereVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere")) ; - if (SphereVisualAsset.Succeeded())
- {
- SphereVisual->SetStaticMesh(SphereVisualAsset.Object);
- SphereVisual->SetRelativeLocation(FVector(0.0f, 0.0f, -40.0f));
- SphereVisual->SetWorldScale3D(FVector(0.8f));
- }
-
- // 创建可激活或停止的粒子系统
- OurParticleSystem = CreateDefaultSubobject
(TEXT("MovementParticles")); - OurParticleSystem->SetupAttachment(SphereVisual);
- //false:一开始不显示效果(没有激活)
- OurParticleSystem->bAutoActivate = false;
- OurParticleSystem->SetRelativeLocation(FVector(-20.0f, 0.0f, 20.0f));
- //从磁盘加载资源
- static ConstructorHelpers::FObjectFinder
- ParticleAsset(TEXT("/Game/StarterContent/Particles/P_Fire.P_Fire"));
- if (ParticleAsset.Succeeded())
- {
- OurParticleSystem->SetTemplate(ParticleAsset.Object);
- }
-
- // 使用弹簧臂给予摄像机平滑自然的运动感。
- USpringArmComponent* SpringArm = CreateDefaultSubobject
(TEXT("CameraAttachmentArm")); - SpringArm->SetupAttachment(RootComponent);
- SpringArm->SetRelativeRotation(FRotator(-45.f, 0.f, 0.f));
- SpringArm->TargetArmLength = 400.0f;
- SpringArm->bEnableCameraLag = true;
- SpringArm->CameraLagSpeed = 3.0f;
-
- // 创建摄像机并附加到弹簧臂
- UCameraComponent* Camera = CreateDefaultSubobject
(TEXT("ActualCamera")); - //将相机放在弹簧臂的插槽
- Camera->SetupAttachment(SpringArm, USpringArmComponent::SocketName);
-
- // 控制默认玩家
- AutoPossessPlayer = EAutoReceiveInput::Player0;
========================================================
1.配置操作映射和轴映射

========================================
创建 移动组件 管理移动
1.创建 移动组件 管理移动,而非直接在Pawn类中处理所有移动。本教程中将扩展 Pawn移动组件 类。
2.与Pawn类不同的,Pawn移动组件类默认为不可见。要找到该组件,需选中 显示所有类(Show All Classes) 选项
3.在搜索栏中输入"movement",以便快速缩小列表范围。

4.由于已将Pawn类命名为 CollidingPawn,则将该类命为 CollidingPawnMovementComponent。

===================================================
为Pawn移动组件的行为编写代码
底层逻辑:用movement组件来更新根组件
1.为自定义Pawn移动组件编写代码。只需编写 TickComponent 函数(类似Actor的 Tick 函数)告知逐帧移动方式。在 CollidingPawnMovementComponent.h 中,需在类定义中覆盖 TickComponent:
- public:
- //override就是可重写的意思
- virtual void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction) override;
2.在 CollidingPawnMovementComponent.cpp 中定义此函数:
- void UCollidingPawnMovementComponent::TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction)
- {
- Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
-
- // 确保所有事物持续有效,以便进行移动。
- if (!PawnOwner || !UpdatedComponent || ShouldSkipUpdate(DeltaTime))
- {
- return;
- }
-
- // 获取(然后清除)ACollidingPawn::Tick中设置的移动向量
- FVector DesiredMovementThisFrame = ConsumeInputVector().GetClampedToMaxSize(1.0f) * DeltaTime * 150.0f;
- if (!DesiredMovementThisFrame.IsNearlyZero())
- {
- FHitResult Hit;
- SafeMoveUpdatedComponent(DesiredMovementThisFrame, UpdatedComponent->GetComponentRotation(), true, Hit);
-
- // 若发生碰撞,尝试滑过去
- if (Hit.IsValidBlockingHit())
- {
- SlideAlongSurface(DesiredMovementThisFrame, 1.f - Hit.Time, Hit.Normal, Hit);
- }
- }
- };
===============================================
1.要使用自定义 Pawn移动组件,首先需将变量添加到 Pawn 类进行追踪
- UPROPERTY()
- class UCollidingPawnMovementComponent* OurMovementComponent;
2.在cpp中引入头文件
#include "CollidingPawnMovementComponent.h"
3.在构造函数中创建移动组件,并且设置组件更新根组件
- // 创建移动组件的实例,并要求其更新根。
- OurMovementComponent = CreateDefaultSubobject
(TEXT("CustomMovementComponent")); - OurMovementComponent->UpdatedComponent = RootComponent;
4.Pawn拥有名为 GetMovementComponent 的函数,用于提供引擎中其他类访问该Pawn当前所用Pawn移动组件的权限。需覆盖该函数,使其返回自定义Pawn移动组件
在 CollidingPawn.h 中的类定义中,需添加:
virtual UPawnMovementComponent* GetMovementComponent() const override;
5.而在 CollidingPawn.cpp 中,需按以下所示,添加覆盖函数的定义:
- UPawnMovementComponent* ACollidingPawn::GetMovementComponent() const
- {
- return OurMovementComponent;
- }
6.创建代码处理Pawn会接收的输入。首先在 CollidingPawn.h 中的类定义内声明以下函数:
- void MoveForward(float AxisValue);
- void MoveRight(float AxisValue);
- void Turn(float AxisValue);
- void ParticleToggle();
7.在 CollidingPawn.cpp 中,按以下所示,添加此类函数的定义:
- void ACollidingPawn::MoveForward(float AxisValue)
- {
- if (OurMovementComponent && (OurMovementComponent->UpdatedComponent == RootComponent))
- {
- OurMovementComponent->AddInputVector(GetActorForwardVector() * AxisValue);
- }
- }
-
- void ACollidingPawn::MoveRight(float AxisValue)
- {
- if (OurMovementComponent && (OurMovementComponent->UpdatedComponent == RootComponent))
- {
- OurMovementComponent->AddInputVector(GetActorRightVector() * AxisValue);
- }
- }
-
- void ACollidingPawn::Turn(float AxisValue)
- {
- FRotator NewRotation = GetActorRotation();
- NewRotation.Yaw += AxisValue;
- SetActorRotation(NewRotation);
- }
-
- void ACollidingPawn::ParticleToggle()
- {
- if (OurParticleSystem && OurParticleSystem->Template)
- {
- OurParticleSystem->ToggleActive();
- }
- }
8.现在只需将函数绑定到输入事件。将下列代码添加到 ACollidingPawn::SetupPlayerInputComponent:
- void ACollidingPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
- {
- Super::SetupPlayerInputComponent(PlayerInputComponent);
- PlayerInputComponent->BindAction("ParticleToggle", IE_Pressed, this, &ACollidingPawn::ParticleToggle);
-
- PlayerInputComponent->BindAxis("MoveForward", this, &ACollidingPawn::MoveForward);
- PlayerInputComponent->BindAxis("MoveRight", this, &ACollidingPawn::MoveRight);
- PlayerInputComponent->BindAxis("Turn", this, &ACollidingPawn::Turn);
-
- }