I’ve been forced to make my own collision detection because unity’s OnCollisionEnter doesn’t seem to be working reliably. So I’ve been casting overlaps every frame in a small radius to check for collision. The only property I see about contact is contact offset. Is there a way I can find out the first point of contact with the overlap?
Code:
internal void FireProjectile(GameObject projectile, float speed)
{
projectile.transform.position =
State.PlayerTransform.position + State.PlayerTransform.forward;
projectile.transform.rotation = State.PlayerTransform.rotation;
m_ProjectileRigidbody = projectile.GetComponent<Rigidbody>();
m_ProjectileRigidbody.AddForce
(State.PlayerTransform.forward * speed, ForceMode.Impulse);
if (State.PlayerState.Consumes)
{
State.PlayerState.ConsumeCellEnergy(EnergyConsumption);
State.PlayerState.GenerateCellHeat(HeatProduction);
}
}
OverlapSphere doesn’t do any collision detection but only an overlap test with the AABB of the collider and not with the collider itself. To detect “collisions” you always need some sort of movement to determine a collision point. If you do an OverlapSphere and another collider is completely inside the sphere, what’s the collision point?
Detecting collisions is a tough topic. For very simple cases it’s possible to roll your own system but for everything else you usually rely on existing system. So did Unity as Unity just uses PhysX for it’s 3d physics and Box2d for the 2d physics.
What do you mean by “not reliably”? What’s your case? Which colliders are involved? Two moving objects or one moving and one stationary? Does the moving object has a non kinematic rigidbody? Do you move the object with physics (either applying forces, setting velocity directly or by using rigidbody.MovePosition)? Moving with Transform.position or Transform.Translate will bypass physics completely. If you’re lucky it might detect a collision the next physics frame when the rigidbody is awake, but the result would be an abrupt “correction” instead if a proper collision response.