U++ 碰撞函数结构
2026/7/17 16:45:39 网站建设 项目流程

碰撞函数

UFUNCTION() void OnBoxBeginOverlap( UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult );

参数详解

UPrimitiveComponent* OverlappedComponent

表示自己这边哪个组件发生了重叠。

碰撞检测组件都继承自UPrimitiveComponent,大致的继承关系为:

UPrimitiveComponent
├── UShapeComponent
│ ├── UBoxComponent
│ ├── USphereComponent
│ └── UCapsuleComponent
├── UStaticMeshComponent
└── USkeletalMeshComponent

什么时候使用它

当有多个碰撞组件被绑定到同一个函数,就可以判断是哪一个触发了事件:

if (OverlappedComponent == FrontBox) { UE_LOG(LogTemp, Warning, TEXT("进入前方区域")); } else if (OverlappedComponent == BackBox) { UE_LOG(LogTemp, Warning, TEXT("进入后方区域")); }

AActor* OtherActor

表示与之发生重叠的另一个完整Actor

使用指针前,一般先检查是否有效,也经常排除自己:

if (!OtherActor || OtherActor == this) { return; }

UPrimitiveComponent* OtherComp

对方Actor中,具体是哪一个组件参与了重叠。

OtherComp和OtherActor的区别:

OtherActor = 对方整个Actor OtherComp = 对方Actor中的具体组件

OtherBodyIndex

该参数一般用不上,但也不能删除。后续有需求再记录该参数的作用。

bFromSweep

该参数是返回bool值,表示这次重叠是否来自带Sweep的移动检测。

什么是Sweep?

如果物体从A点移动到B点,不使用Sweep的话,可以理解为直接把物体放到新位置,不沿途扫描障碍物。使用Sweep可以理解为从旧位置移动到新位置时,检测图中是否与碰撞体发生接触。

const FHitResult& SweepResult

如果本次重叠由Sweep移动产生,这个参数保存对应的查询结果。

FHitResult是 UE 中保存碰撞或 Trace 命中信息的结构体,可以包含位置、碰撞法线、命中的 Actor 和组件等信息。常见的成员包括:

SweepResult.Location SweepResult.ImpactPoint SweepResult.Normal SweepResult.ImpactNormal SweepResult.GetActor() SweepResult.GetComponent() SweepResult.bBlockingHit SweepResult.bStartPenetrating


void OnBoxEndOverlap( UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex );

EndOverlap中的四个参数和BeginOverlap中的前四个参数完全对应。



需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询