In this lesson, you will learn how to extract specific physical details from the Collision object to make your gameplay interactions feel responsive and grounded.
When two objects collide in Unity, the OnCollisionEnter method provides a Collision parameter. While we often use this just to check if a collision happened, the real power lies in the data stored inside that object. This data allows you to create dynamic feedback, such as louder sound effects for harder hits or visual impact markers at the exact point of contact.
Extracting Collision Data
The Collision object acts as a container for metadata about the impact. Here are the three most useful properties:
relativeVelocity: This tells you how fast the two objects were moving relative to each other at the moment of impact. You can use this to scale your feedback—a heavy ball hitting a wall at high speed should trigger a stronger vibration or sound than a light tap.impulse: This represents the total force applied during the collision. It is perfect for determining the "weight" or "intensity" of the impact.contacts: This is an array ofContactPointobjects. Each point contains apoint(the Vector3 position in world space) and anormal(the direction of the surface struck).
Practical Example
Here is how you might use this data to spawn an effect at the correct location:
void OnCollisionEnter(Collision collision)
{
// Use the first contact point to spawn an effect
ContactPoint contact = collision.contacts[0];
Instantiate(impactEffectPrefab, contact.point, Quaternion.identity);
// Use relative velocity to determine the intensity of a sound
float hitStrength = collision.relativeVelocity.magnitude;
audioSource.PlayOneShot(hitClip, Mathf.Clamp01(hitStrength / 10f));
}
By utilizing contact.point, your effects will always appear exactly where the objects touched, rather than at the center of the GameObject. Similarly, by mapping relativeVelocity to your audio volume or particle emission rate, your game world will respond naturally to the chaos of physics.
Key Takeaway: The Collision object is a data-rich report of the impact; use relativeVelocity for intensity, impulse for force magnitude, and the contacts array to pinpoint the exact location of the interaction.