创建一个漂浮物,放在场景中,子弹击中就会消失。
首先是添加静态网格体变量
UPROPERTY(VisibleAnywhere , Category = "Static")
UStaticMeshComponent* VisualMesh;
添加检查actor的函数声明
UFUNCTION()
void CheckActor( UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult);
在源文件中引用头文件
#include "FloatingActor.h"
//#include"Components/PrimitiveComponent.h"
#include"Components/StaticMeshComponent.h"
#include"FPSGame/FPSGameProjectile.h"
然后在构造函数中进行初始化,并设置重叠事件的激活
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
VisualMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
//actor的重叠事件的激活!!!!!!!!!!!!
VisualMesh->OnComponentBeginOverlap.AddDynamic(this , &AFloatingActor::CheckActor);
VisualMesh->SetupAttachment(RootComponent);
static ConstructorHelpers::FObjectFinder<UStaticMesh> CubeVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Cube.Shape_Cube"));
if (CubeVisualAsset.Succeeded())
{
VisualMesh->SetStaticMesh(CubeVisualAsset.Object);
VisualMesh->SetRelativeLocation(FVector(0.0f, 0.0f, 0.0f));
}
定义成员函数
void AFloatingActor::CheckActor( UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)
{
//将碰撞漂浮物的另一个actor的指针转化为子弹类型的指针
AFPSGameProjectile * Projectile = Cast<AFPSGameProjectile>(OtherActor);
//如果转化失败,说明不是子弹碰撞的漂浮物
if (Projectile == nullptr)
{
return;
}
//否则就是子弹击中了漂浮物
else
{
this->Destroy();
}
}
打开编辑器,点击场景中的漂浮物
设置碰撞预设
测试 成功