Search code examples
collision-detectionunreal-engine4overlapunreal-engine5

In Unreal, can a sphere component generate both Hit and BeginOverlap event at the same time when colliding with the floor or a wall?


Let's say I have a rolling ball in my Unreal engine project. I want to detect when the ball is touching the floor and when it's not (like I could do with OnCollisionEnter and OnCollisionExit in Unity).

To do that in Unreal, do I need 2 sphere components, one to collide with the floor that will trigger OnComponentHit event and another one little bit bigger that will overlap the floor and trigger OnComponentOverlapEvent? Or I can use a single sphere component that will collide with the floor and at the same time will trigger both events?


Solution

  • OnComponentOverlap and OnComponentHit can indeed be generated by the same UPrimitiveComponent such as a sphere collision. However, usually not with the same object: depending on the collision settings, the sphere would either "hit" the floor or "overlap" it.

    OnHit gets called if the component is set to "block" collision and is doing a physics simulation, and OnComponentOverlap if it's set to "overlap", which would mean there's no physics interaction and they can go through each other. If it's set to "ignore", neither will trigger.

    It can happen that the same component is set to "block" some things and "overlap" others, in which case both events will be called at different times.

    If you need to have a sphere that both triggers hit events when exactly touching the floor, but also overlap events when it's roughly on the floor, you can indeed use two different components, with different collision settings, and slightly different sizes.

    Most of the information in the OnHit and OnOverlap events is quite similar, so depending on the exact use case, it might be possible to get by with just one. However, for sphere-plane collision (and any primitive-primitive collision like capsule-box), the overlap calculation is extremely cheap, so there's virtuall no performance difference between using one and two.