UE4学习笔记17th:绑定Pawn和Components

来源:互联网 发布:景观大数据的课程如何 编辑:程序博客网 时间:2024/06/05 09:52

在CollidingPawn.h中添加:

//跟踪PawnMovementComponentclass UCollidingPawnMovementComponent * OurMovementComponent;

这里写图片描述

添加头文件在CollidingPawn.pp中添加头文件:

#include "CollidingPawnMovementComponent.h"

这里写图片描述

在ACollidingPawn::ACollidingPAwn中添加:

//为运动组件创建实例,并更新根。Create an instance of our movement component, and tell it to update the root.OurMovementComponent = CreateDefaultSubobject<UCollidingPawnMovementComponent>(TEXT("CustMovementComponent"));OurMovementComponent->UpdatedComponent = RootComponent;

这里写图片描述

在CollidingPawn.h中添加:

virtual UPawnMovementComponent* GetMovementComponent() const override;

在CollidingPawn.cpp中添加:

 UPawnMovementComponent* ACollidingPawn::GetMovementComponent() const{return OurMovementComponent;}

下面开始处理输入
在CollidingPawn.h中添加:

//处理输入void MoveForward(float AxisValue);void MoveRight(float AxisValue);void Turn(float AxisValue);void ParticleToggle();//

在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();}}//

接下来就剩绑定了

InputComponent->BindAction("ParticleToggle", IE_Pressed, this, &ACollidingPawn::ParticleToggle);InputComponent->BindAxis("MoveForward", this, &ACollidingPawn::MoveForward);InputComponent->BindAxis("MoveRight", this, &ACollidingPawn::MoveRight);InputComponent->BindAxis("Turn", this, &ACollidingPawn::Turn);

如此,代码部分就完成了。

完成的代码

CollidingPawn.h// Fill out your copyright notice in the Description page of Project Settings.#pragma once#include "GameFramework/Pawn.h"#include "CollidingPawn.generated.h"UCLASS()class HOWTO_COMPONENTS_API ACollidingPawn : public APawn{GENERATED_BODY()public:// Sets default values for this pawn's propertiesACollidingPawn();// Called when the game starts or when spawnedvirtual void BeginPlay() override;// Called every framevirtual void Tick( float DeltaSeconds ) override;// Called to bind functionality to inputvirtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;//追溯粒子系统组件UParticleSystemComponent *OurParticleSystem;////跟踪PawnMovementComponentclass UCollidingPawnMovementComponent * OurMovementComponent;virtual UPawnMovementComponent* GetMovementComponent() const override;//处理输入void MoveForward(float AxisValue);void MoveRight(float AxisValue);void Turn(float AxisValue);void ParticleToggle();//};CollidingPawn.cpp// Fill out your copyright notice in the Description page of Project Settings.#include "HowTo_Components.h"#include "CollidingPawn.h"#include "CollidingPawnMovementComponent.h"// Sets default valuesACollidingPawn::ACollidingPawn(){ // 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;//创建一个可以对物理行为进行响应的球体根组件USphereComponent* SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("RootComponent"));RootComponent = SphereComponent;SphereComponent->InitSphereRadius(40.0f);SphereComponent->SetCollisionProfileName(TEXT("Pawn"));////创建并放置网格组件,这样我们能看到球体的位置UStaticMeshComponent* SphereVisual = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("VisualRepresentation"));SphereVisual->AttachTo(RootComponent);static ConstructorHelpers::FObjectFinder<UStaticMesh> 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<UParticleSystemComponent>(TEXT("MovementParticles"));OurParticleSystem->AttachTo(SphereVisual);OurParticleSystem->bAutoActivate = false;OurParticleSystem->SetRelativeLocation(FVector(-20.0f, 0.0f, 20.0f));static ConstructorHelpers::FObjectFinder<UParticleSystem> ParticleAsset(TEXT("/Game/StarterContent/Particles/P_Fire.P_Fire"));if (ParticleAsset.Succeeded()){OurParticleSystem->SetTemplate(ParticleAsset.Object);}////使用弹簧臂使相机平滑、自然的运动USpringArmComponent* SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraAttachmentArm"));SpringArm->AttachTo(RootComponent);SpringArm->RelativeRotation = FRotator(-45.f, 0.f, 0.f);SpringArm->TargetArmLength = 400.0f;SpringArm->bEnableCameraLag = true;SpringArm->CameraLagSpeed = 3.0f;////创建相机并附加到弹簧臂UCameraComponent* Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("ActualCamera"));Camera->AttachTo(SpringArm, USpringArmComponent::SocketName);////控制默认玩家AutoPossessPlayer = EAutoReceiveInput::Player0;////为运动组件创建实例,并更新根。Create an instance of our movement component, and tell it to update the root.OurMovementComponent = CreateDefaultSubobject<UCollidingPawnMovementComponent>(TEXT("CustMovementComponent"));OurMovementComponent->UpdatedComponent = RootComponent;}// Called when the game starts or when spawnedvoid ACollidingPawn::BeginPlay(){Super::BeginPlay();}// Called every framevoid ACollidingPawn::Tick( float DeltaTime ){Super::Tick( DeltaTime );}// Called to bind functionality to inputvoid ACollidingPawn::SetupPlayerInputComponent(class UInputComponent* InputComponent){Super::SetupPlayerInputComponent(InputComponent);InputComponent->BindAction("ParticleToggle", IE_Pressed, this, &ACollidingPawn::ParticleToggle);InputComponent->BindAxis("MoveForward", this, &ACollidingPawn::MoveForward);InputComponent->BindAxis("MoveRight", this, &ACollidingPawn::MoveRight);InputComponent->BindAxis("Turn", this, &ACollidingPawn::Turn);}UPawnMovementComponent* ACollidingPawn::GetMovementComponent() const{return OurMovementComponent;}//控制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();}}//CollidingPawnMovementComponent.h // Fill out your copyright notice in the Description page of Project Settings.#pragma once#include "GameFramework/PawnMovementComponent.h"#include "CollidingPawnMovementComponent.generated.h"/** *  */UCLASS()class HOWTO_COMPONENTS_API UCollidingPawnMovementComponent : public UPawnMovementComponent{GENERATED_BODY()public:virtual void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction) override;};CollidingPawnMovementComponent.cpp// Fill out your copyright notice in the Description page of Project Settings.#include "HowTo_Components.h"#include "CollidingPawnMovementComponent.h"void UCollidingPawnMovementComponent::TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction){Super::TickComponent(DeltaTime, TickType, ThisTickFunction);//Make sure that everything is still valid, and that we are allowed to move.if (!PawnOwner || !UpdatedComponent || ShouldSkipUpdate(DeltaTime)){return;}////Get (and then clear) the movement vector that we set in ACollidingPawn::TickFVector DesiredMovementThisFrame = ConsumeInputVector().GetClampedToMaxSize(1.0f) * DeltaTime * 150.0f;if (!DesiredMovementThisFrame.IsNearlyZero()){FHitResult Hit;SafeMoveUpdatedComponent(DesiredMovementThisFrame, UpdatedComponent->GetComponentRotation(), true, Hit);//If we bumped into something, try to slide along itif (Hit.IsValidBlockingHit()){SlideAlongSurface(DesiredMovementThisFrame, 1.f - Hit.Time,Hit.Normal,Hit);}}};
原创粉丝点击