// .h文件 #include "CoreMinimal.h" #include "Engine/StaticMeshActor.h" #include "TargetStaticMeshActor.generated.h" /** * */ UCLASS() class LEAMCPP_API ATargetStaticMeshActor : public AStaticMeshActor { GENERATED_BODY() public: ATargetStaticMeshActor(); virtual void NotifyHit(class UPrimitiveComponent* MyComp, AActor* Other, class UPrimitiveComponent* OtherComp, bool bSelfMoved, FVector HitLocation, FVector HitNormal, FVector NormalImpulse, const FHitResult& Hit) override; };
// CPP文件 #include "TargetStaticMeshActor.h" #include "Engine/Engine.h" ATargetStaticMeshActor::ATargetStaticMeshActor() { } //如果发生碰撞则调用这个函数 void ATargetStaticMeshActor::NotifyHit(class UPrimitiveComponent* MyComp, AActor* Other, class UPrimitiveComponent* OtherComp, bool bSelfMoved, FVector HitLocation, FVector HitNormal, FVector NormalImpulse, const FHitResult& Hit) { Super::NotifyHit(MyComp, Other, OtherComp, bSelfMoved, HitLocation, HitNormal, NormalImpulse, Hit); if (Other == nullptr) return;//如果没有碰撞到其他actor,返回; if (GEngine)//如果包含日志文件 { GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("NotifyHit"));//打印到屏幕,左上角,保留15s,颜色为黄色,文字。。。 } }