22,攻击检测改为C++

回到动画源中

进行攻击检测的动画通知改写

这里用到了蓝图接口

把蓝图接口改为C++

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"

#include "UObject/Interface.h"

#include "Combat.generated.h"

// This class does not need to be modified.

UINTERFACE(MinimalAPI)

class UCombat : public UInterface

{

GENERATED_BODY()

};

/**

*

*/

class ROUGHLIKE1_API ICombat

{

GENERATED_BODY()

复制代码
// Add interface functions to this class. This is the class that will be inherited to implement this interface.

public:

//攻击重置

UFUNCTION(BlueprintNativeEvent, Category = "Combat")

void INT_ResetAtk();

virtual void INT_ResetAtk_Implementation() = 0;

复制代码
//攻击检测
UFUNCTION(BlueprintNativeEvent, Category = "Combat")
void INT_AttackDetection(FVector boxPosition, FVector boxExtension);
virtual void INT_AttackDetection_Implementation(FVector boxPosition, FVector boxExtension) = 0;

};

再写动画通知类

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"

#include "Notifies/PaperZDAnimNotify.h"

#include "ANZ_Attack.generated.h"

/**

*

*/

UCLASS()

class ROUGHLIKE1_API UANZ_Attack : public UPaperZDAnimNotify

{

GENERATED_BODY()

public:

UANZ_Attack();

//攻击盒大小

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Attack")

FVector BoxExtent;

//攻击位置的Socket名称

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Attack")

FName socketName;

protected:

//重写动画通知的触发事件

virtual void OnReceiveNotify_Implementation(UPaperZDAnimInstance* OwningInstance) const override;

};

// Fill out your copyright notice in the Description page of Project Settings.

#include "AnimNotify/ANZ_Attack.h"

#include <Interface/Combat.h>

#include <PaperZDAnimInstance.h>

#include <MyPaperZDCharacter.h>

UANZ_Attack::UANZ_Attack()

{

BoxExtent = FVector(100.0f, 50.0f, 50.0f);

socketName = TEXT("Socket_ATK");

}

void UANZ_Attack::OnReceiveNotify_Implementation(UPaperZDAnimInstance* OwningInstance) const

{

if (!OwningInstance)

{

return;

}

//获取动画实例所属的actor

AActor* ownerActor = OwningInstance->GetOwningActor();

if (!ownerActor)

{

return;

}

//获取渲染组件,用来读取Socket位置

UActorComponent* ac = ownerActor->GetComponentByClass(USceneComponent::StaticClass());

if (!ac)

{

return;

}

USceneComponent* RenderComp = Cast(ac);

if (!RenderComp)

{

return;

}

//获取socket的世界位置

FVector socketLocation = RenderComp->GetSocketLocation(socketName);

if (ownerActor->Implements())

{

ICombat::Execute_INT_AttackDetection(ownerActor,socketLocation, BoxExtent);

}

}

由于这个蓝图接口是角色类调用的,

所以,在角色类重载

复制代码
virtual void INT_AttackDetection_Implementation(FVector boxPosition, FVector boxExtension) override;

由于还用到了其他蓝图接口,所以暂时让蓝图实现,即

复制代码
// 扩展入口:蓝图 / C++ 均可重写,
UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "Combat | Attack")
void ProcessHitResult(AActor* Target, float Force);
virtual void ProcessHitResult_Implementation(AActor* Target, float Force) {}

void AMyPaperZDCharacter::INT_AttackDetection_Implementation( FVector boxPosition, FVector boxExtension)

{

//如果死亡,则返回

if (IsDead)

{

return;

}

//构建BoxOverlap参数

TArray<TEnumAsByte> objectTypes;

objectTypes.Add(EObjectTypeQuery::ObjectTypeQuery3); //pawn类型

复制代码
//忽略玩家自身
TArray<AActor*> actorsToIgnore;
actorsToIgnore.Add(this);
//执行盒体检测
TArray<AActor*> outActors;
bool bHasOverlap = UKismetSystemLibrary::BoxOverlapActors(
    this,
    boxPosition,
    boxExtension,
    objectTypes,
    nullptr,
    actorsToIgnore,
    outActors
);
if (!bHasOverlap)
{
    return;
}
//根据连击数设置推力
float force = (comboIndex == 0) ? 200.0f : 600.0f;
//遍历所有重叠actor
for (AActor* target : outActors  )
{
    if (!target)
    {
        continue;
    }
    bool bIsEnemy = target->ActorHasTag(FName("enemy"));
    if (!bIsEnemy)
    {
        continue;
    }
    this->ProcessHitResult(target, force);
}

}

在蓝图中暂时保留

相关推荐
日月云棠3 天前
UE5 Lyra源码分析——Audio_Analysis音频模块全面分析
ue5·音视频
成都渲染101云渲染66663 天前
2026云渲染平台哪个好?建筑、动画、UE5项目选择云渲染的3个关键标准
ue5
清泓y4 天前
UE 物理系统知识分享
面试·ue5·游戏程序
清泓y4 天前
UE移动开发技术面试题
android·面试·ue5·ue4·游戏程序
清泓y4 天前
UE基础知识与引擎架构面试题
面试·架构·ue5·ue4·游戏程序
mengzhi啊6 天前
UE5关卡切换 传统加载(阻塞)vs流式加载(非阻塞)。还有加载关卡3种办法
ue5
电子云与长程纠缠6 天前
UE中使用TGuardValue与TInlineComponentArray数据结构
开发语言·数据结构·学习·ue5·游戏引擎
Duo1J8 天前
【UE】Slate 编辑器工具开发03 - 节点编辑器 (EdGraph)
ue5·编辑器·游戏引擎·ue4
directx3d_beginner9 天前
26,怪物受击接口改为C++
ue5
远离UE411 天前
UE5 UV邻接 Niagara潜水模拟 学习笔记
ue5·uv