Detecting an imminent collision?

I think I get collision detection in Unity on a basic level,

but what I’d like to know is how to check whether a collision is about to happen (during the next frame, the next N frames, or whatever).

The way I’d do this in other engines might be to have a larger hitbox around the object and have that trigger the aboutToCollide method or whatever.

I’m not sure whether there’s a best practice for this in Unity?


Same thing can be done in Unity, give the object another hitbox and use “OnCollisionEnter” function.

or you could use raycasting to predict what is going to collide

RaycastHit rayHit;
Vector3 rayOrigin = this.transform.position;
Vector3 ray = objectsVelocity * predictionRange;

if(Physics.Raycast(rayOrigin, ray, out rayHit, ray.magnitude))
    // The ray has hit the object will hit in "predictionRange" number of frames if its velocity remains constant.


PredictionRange would be the number of frames ahead you want to see. So you multiply your objects velocity 10 times, you will see impacts 10 frames ahead of your object. Of course a ray is just a line, if your object is very wide there might be collisions you miss. You need to move an invisible trigger ahead of you as suggested above in this case but I dont like this method as it could be possible to get an object between you and the dummy collider.