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

}

在蓝图中暂时保留

相关推荐
HAPPY酷2 天前
【ROS2】 机械臂架构 vs UE5 游戏开发架构 终极对照表
架构·ue5
DoomGT2 天前
UE5 - 制作游戏中切换窗口后游戏自动暂停功能
游戏·ue5·ue4·虚幻·虚幻引擎·unreal engine
远离UE44 天前
UE5 World Partition
ue5
远离UE45 天前
UE5 简单 Mesh Shader 制作流程
ue5
梦帮科技6 天前
UE5 GAS 实战:用 Gameplay Ability System 搭建「赛博修真」境界与技能体系
c++·人工智能·python·ue5·c#
远离UE46 天前
UE5 各类型灯光学习
学习·ue5
四代水门1 个月前
UE5实现客户端与服务器时间同步
ue5
智海深蓝1 个月前
海上平行战场:态势模拟三维可视化平台
3d·ue5
_守一1 个月前
UE5 ListView记录
ue5