Tips
-
SpringArm->bEnableCameraLag = true; -
SpringArm->CameraLagSpeed = 3.0f; -
OurParticleSystem->SetTemplate(ParticleAsset.Object); 用于更改粒子系统的函数,相较于网格体是SetStaticMesh() -
Camera->SetupAttachment(SpringArm,USpringArmComponent::SocketName);
void SetupAttachment
(
USceneComponent * InParent,//要附加到的父组件
FName InSocketName//插槽名
)
此处的就是对应的插槽名
AutoPossessPlayer = EAutoReceiveInput::Player0; 设置吸收输入的pawn,添加多个此类pawn,Player0取得是最新添加到场景中的pawn.OurMovementComponent->UpdatedComponent = RootComponent; ShouldUpdateComponent避免组件的无意义渲染AddInputVector(GetActorForwardVector() * AxisValue) NewRotator.Yaw
自定义继承的移动组件MovementComponent
继承自PawnMovementComponent,并声明重写了TickComponent
源码
CollidingPawn.h
......
//粒子效果组件
UParticleSystemComponent * OurParticleSystem;
//移动的组件
class UCollidingPawnMovementComponent * OurMovementComponent;
//virtual UPawnMovementComponent* GetMovementComponent() const override;
//移动函数
void MoveForward(float AxisValue);
void MoveRight(float AxisValue);
void Turn(float AxisValue);
void MoveUp(float AxisValue);
void MoveDown(float AxisValue);
//启动特效的函数
void ParticleToggle();
CollidingPawn.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "CollidingPawn.h"
#include "Camera/CameraComponent.h"
#include "CollidingPawnMovementComponent.h"
#include "Components/InputComponent.h"
#include "Components/StaticMeshComponent.h"
#include "Components/SphereComponent.h"
//#include "ConstructorHelpers.h"
#include "GameFramework/SpringArmComponent.h"
#include "Particles/ParticleSystemComponent.h"
//#include "GameFramework/Controller.h"
// Sets default values
ACollidingPawn::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"));
//SphereComponent->SetSimulatePhysics(true);
//添加网格体
UStaticMeshComponent * SphereComp = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("VisualRepresentation"));
SphereComp->SetupAttachment(RootComponent);
static ConstructorHelpers::FObjectFinder<UStaticMesh> SphereVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere"));
if(SphereVisualAsset.Succeeded())
{
SphereComp->SetStaticMesh(SphereVisualAsset.Object);
SphereComp->SetRelativeLocation(FVector(0.0f,0.0f,-40.0f));
SphereComp->SetWorldScale3D(FVector(0.8f));
}
//粒子效果
OurParticleSystem = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("MovementParticles"));
OurParticleSystem->SetupAttachment(SphereComp);
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("CameraArm"));
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<UCameraComponent>(TEXT("Camera"));
//
Camera->SetupAttachment(SpringArm,USpringArmComponent::SocketName);
//
AutoPossessPlayer = EAutoReceiveInput::Player0;
//添加自定义的移动的组件
OurMovementComponent = CreateDefaultSubobject<UCollidingPawnMovementComponent>(TEXT("CustomMovementComponent"));
OurMovementComponent->UpdatedComponent = RootComponent;
}
// Called when the game starts or when spawned
void ACollidingPawn::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ACollidingPawn::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void ACollidingPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
InputComponent->BindAction("Particle Toggle",IE_Pressed,this,&ACollidingPawn::ParticleToggle);
//InputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
//InputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
InputComponent->BindAxis("MoveForward",this,&ACollidingPawn::MoveForward);
InputComponent->BindAxis("MoveRight",this,&ACollidingPawn::MoveRight);
InputComponent->BindAxis("Turn",this,&ACollidingPawn::Turn);
}
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 NewRotator = GetActorRotation();
NewRotator.Yaw += AxisValue;
SetActorRotation(NewRotator);
}
void ACollidingPawn::ParticleToggle()
{
if(OurParticleSystem && OurParticleSystem->Template)
{
OurParticleSystem->ToggleActive();
}
}
void ACollidingPawn::MoveUp(float AxisValue)
{
if(OurMovementComponent && (OurMovementComponent->UpdatedComponent == RootComponent))
{
OurMovementComponent->AddInputVector(GetActorUpVector() * AxisValue);
}
}
void ACollidingPawn::MoveDown(float AxisValue)
{
if(OurMovementComponent && (OurMovementComponent->UpdatedComponent == RootComponent))
{
OurMovementComponent->AddInputVector(GetActorUpVector() * AxisValue);
}
}
CollidingPawnMovementComponent.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/PawnMovementComponent.h"
#include "CollidingPawnMovementComponent.generated.h"
/**
*
*/
UCLASS()
class THIRDPERSON_3_API UCollidingPawnMovementComponent : public UPawnMovementComponent
{
GENERATED_BODY()
public:
//bool ShouldSkipUpadte(float DeltaTime);
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
};
CollidingPawnMovementComponent.cpp
#include "CollidingPawnMovementComponent.h"
void UCollidingPawnMovementComponent::TickComponent(float DeltaTime, ELevelTick TickType,
FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
if(!PawnOwner || !UpdatedComponent || ShouldSkipUpdate(DeltaTime))
{
return;
}
FVector DesiredMovementThisFrame = ConsumeInputVector().GetClampedToMaxSize(1.0f) * DeltaTime * 150.f;
if(!DesiredMovementThisFrame.IsNearlyZero())
{
FHitResult Hit;
SafeMoveUpdatedComponent(DesiredMovementThisFrame,UpdatedComponent->GetComponentRotation(),true,Hit);
if(Hit.IsValidBlockingHit())
{
SlideAlongSurface(DesiredMovementThisFrame,1.f - Hit.Time,Hit.Normal,Hit);
}
}
}
|