UE4 C++ 碰撞检测(Overlap)

来源:互联网 发布:芜湖网络推广公司 编辑:程序博客网 时间:2024/06/05 05:54

在ue4中 蓝图里的碰撞检测是一个事件,在ue4 c++中 需要先写一个函数
函数签名如下

        void OnHit(class UPrimitiveComponent* OverLapComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult);

具体在头文件中实现的话必须加上UFUNCTION()

UFUNCTION()        void OnHit(class UPrimitiveComponent* OverLapComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult);

在CPP文件中

Box->OnComponentBeginOverlap.AddDynamic(this,&AMyActor::OnHit);

进行动态绑定后

在OnHit()函数下可以实现某些功能如下面的例子中,当发生碰撞时则打印检测到的物体的名字

void AMyActor::OnHit(UPrimitiveComponent * OverLapComp, AActor * OtherActor, UPrimitiveComponent * OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult){    GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::Red, SweepResult.GetActor()->GetName());}

如果没有发生碰撞检测则需要设置物体碰撞类型,两个物体都接受Overlap事件即可

Box->SetCollisionProfileName(TEXT("OverlapAll"));
原创粉丝点击