添加头文件
#include "Components/StaticMeshComponent.h"
禁用网格体组件的碰撞效果
MeshComp->SetCollisionEnabled(ECollisionEnabled::NoCollision);
对球体进行自定义设置,将物理效果设置为仅查询(包括追踪轨迹线或重叠事件)
SphereComp->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
将碰撞设置为响应所有通道,并选择忽略
SphereComp->SetCollisionResponseToAllChannels(ECR_Ignore);
启用Pawn响应通道,设置为发生重叠
SphereComp->SetCollisionResponseToChannel(ECC_Pawn, ECR_Overlap);
新建一个粒子系统,并暴露给蓝图,模式为仅编辑器默认
UPROPERTY(EditDefaultsOnly, Category = "Effects") UParticleSystem* PickupFX;
新建一个播放粒子的函数
void AFPSObjectiveActor::PlayEffects() { UGameplayStatics::SpawnEmitterAtLocation(this, PickupFX, GetActorLocation()); }
重写父类Actor中的NotifyActorBeginOverlap函数
virtual void NotifyActorBeginOverlap(AActor* OtherActor) override;
实现这个函数,通过super关键字来调用父类实现,然后调用PlayEffects()
void AFPSObjectiveActor::NotifyActorBeginOverlap(AActor * OtherActor) { Super::NotifyActorBeginOverlap(OtherActor); PlayEffects(); }
在BeginPlay()中同样也调用PlayEffects()
void AFPSObjectiveActor::BeginPlay() { Super::BeginPlay(); PlayEffects(); }
打开BP_Objective,选择一个粒子特效