IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> 【UE4】使用自定义继承MovementComponent组件的Pawn(粒子效果,自定义移动) -> 正文阅读

[游戏开发]【UE4】使用自定义继承MovementComponent组件的Pawn(粒子效果,自定义移动)

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);
		}
		
	}
}

  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2022-01-17 11:47:50  更:2022-01-17 11:48:57 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/27 18:27:16-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码