UE5/C++ 基于GAS创建攻击伤害 5.1.1准备碰撞体

1.准备碰撞体基类

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "HitCollision.generated.h"

/**
 * 
 */

UCLASS(BlueprintType, Blueprintable)
class SIMPLECOMBAT_API AHitCollision : public AActor
{
	GENERATED_BODY()

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "HitCollision", meta = (AllowPrivateAccess = "true"))
	class USceneComponent* HitCollisionRootComponent;
public:
	AHitCollision(const FObjectInitializer& ObjectInitializer);

	virtual UPrimitiveComponent* GetHitDamage();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

	// Called every frame
	virtual void Tick(float DeltaTime) override;
};

进行实现

#include "Hit/Core/HitCollision.h"

AHitCollision::AHitCollision(const FObjectInitializer& ObjectInitializer) :Super(ObjectInitializer)
{
	//创建根组件
	HitCollisionRootComponent = CreateDefaultSubobject(TEXT("CollisionRootComponent"));
	RootComponent = HitCollisionRootComponent;
}

UPrimitiveComponent* AHitCollision::GetHitDamage()
{
	return NULL;
}

void AHitCollision::BeginPlay()
{
	Super::BeginPlay();
}

void AHitCollision::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
}

2.继承至基类,创建盒体碰撞体

#pragma once

#include "CoreMinimal.h"
#include "Hit/Core/HitCollision.h"
#include "HitBox.generated.h"

/**
 * 
 */
UCLASS(BlueprintType, Blueprintable)
class SIMPLECOMBAT_API AHitBoxCollision : public AHitCollision
{
	GENERATED_BODY()

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "BaseAttrubute", meta = (AllowPrivateAccess = "true"))
	class UBoxComponent* HitDamage;
public:
	AHitBoxCollision(const FObjectInitializer& ObjectInitializer);

	virtual UPrimitiveComponent* GetHitDamage();

	//设置盒子尺寸
	void SetBoxExtent(const FVector& InNewBoxExtent);
protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

	// Called every frame
	virtual void Tick(float DeltaTime) override;
};

进行实现


#include "Hit/ComboSkillHitBox.h"
#include "Components/BoxComponent.h"


AHitBoxCollision::AHitBoxCollision(const FObjectInitializer& ObjectInitializer):Super(ObjectInitializer)
{
	HitDamage = CreateDefaultSubobject(TEXT("HitDamage"));
	HitDamage->SetupAttachment(RootComponent);
}

UPrimitiveComponent* AHitBoxCollision::GetHitDamage()
{
	return HitDamage;
}

void AHitBoxCollision::SetBoxExtent(const FVector& InNewBoxExtent)
{
	HitDamage->SetBoxExtent(InNewBoxExtent);
}

void AHitBoxCollision::BeginPlay()
{
	Super::BeginPlay();
}

void AHitBoxCollision::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
}

3.继承至基类,创建胶囊体碰撞体

#pragma once

#include "CoreMinimal.h"
#include "Hit/Core/HitCollision.h"
#include "HitCapsule.generated.h"
/**
 * 
 */
UCLASS(BlueprintType, Blueprintable)
class SIMPLECOMBAT_API AHitCapsuleCollision : public AHitCollision
{
	GENERATED_BODY()


	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "BaseAttrubute", meta = (AllowPrivateAccess = "true"))
	class UCapsuleComponent*HitDamage;
public:
	AHitCapsuleCollision(const FObjectInitializer& ObjectInitializer);


	virtual UPrimitiveComponent* GetHitDamage();

	void SetCapsuleHalfHeight(float InNewCapsuleHalfHeight);
	void SetCapsuleRadius(float InNewCapsuleRadius);
protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

	// Called every frame
	virtual void Tick(float DeltaTime) override;
};

进行实现

#include "Hit/ComboSkillHitCapsule.h"
#include "Components/CapsuleComponent.h"

AHitCapsuleCollision::AHitCapsuleCollision(const FObjectInitializer& ObjectInitializer) :Super(ObjectInitializer)
{
	HitDamage = CreateDefaultSubobject(TEXT("HitDamage"));
	HitDamage->SetupAttachment(RootComponent);
}

UPrimitiveComponent* AHitCapsuleCollision::GetHitDamage()
{
	return HitDamage;
}

void AHitCapsuleCollision::SetCapsuleHalfHeight(float InNewCapsuleHalfHeight)
{
    //设置胶囊体半高
	HitDamage->SetCapsuleHalfHeight(InNewCapsuleHalfHeight);
}

void AHitCapsuleCollision::SetCapsuleRadius(float InNewCapsuleRadius)
{
    //设置胶囊体半径
	HitDamage->SetCapsuleRadius(InNewCapsuleRadius);
}

void AHitCapsuleCollision::BeginPlay()
{
	Super::BeginPlay();
}

void AHitCapsuleCollision::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
}

4.继承至基类,创建球体碰撞体

你可能感兴趣的:(ue5,c++,ue4)