Understanding OnCollision methods in Unity

In this post, we’ll talk about Unity methods related to collisions. So fasten your seatbelt as we are heading into collision! 🏎

Currently, in Unity, there are two separate physics engines. One for 3D physics, and another one for 2D. Both work in the very same manner, but they are using different components.

So for example, if we want to turn an object into a physical object in 3D space, we would use Rigidbody. Analogous we would use Rigidbody2D if we want to turn it into a 2D physical object.

In Unity, we have quite a few methods that we can override. Here we have three related to the collisions. OnCollisionEnter(), OnCollisionStay() and OnCollisionExit(). These methods work with the 3D physic engine, but there are also their 2D equivalents, OnCollisionEnter2D(), OnCollisionStay2D() and OnCollisionExit2D().

When they are called?

Before we jump into these methods, there is an important thing that you need to know. These methods are invoked only when two objects have Collider component attached to them, and at least one has Rigidbody! Of course, they also need to collide with one another, and the same is true with 2D components. πŸ˜…

2D and 3D physics engines don’t interact with each other so keep this in your mind.

Beginning with OnCollisionEnter() is called once when objects collide with each other. This is abvious as the name of method is saying that. πŸ˜…

OnCollisionStay() is called after two objects collide and they are touching each other. This method is invoked as long as objects are touching or they will stop moving, and Unity decides to put them to sleep mode. 😴

And the last one, OnCollisionExit() is invoked when objects won’t collide with each other anymore. πŸ‘‹

When to use each of them?

So the obvious question is when or for what should you use each of them?

You can use OnCollisionEnter() for things like mines or other explosives when you want to make an explosion when something hits the object. Another example could be shattering of the fragile object like glass when something hit it. Of course, it also can be used to play sound effects!

Example for OnCollisionStay() can be shooting sparks when two metal objects are rubbing against one another. Other could be something like setting a flag for a character with is moving on the ground. Another could be a platform in a puzzle game.

OnCollisionExit() can be used for stopping or finishing things that I presented you in OnCollisionEnter() and OnCollisionStay().

Exercise for you

Use a script from bellow and attach it to a few objects on the scene. See when they are called and for how long.

using UnityEngine;

/// <summary>
/// This class prints debug logs for OnCollision methods.
/// </summary>
public class CollisionTester : MonoBehaviour
{
    /// <summary>
    /// Unity method called when two objects collide with each other.
    /// </summary>
    /// <param name="collision">Collision.</param>
    private void OnCollisionEnter(Collision collision)
    {
        Debug.LogFormat("{0} collision enter: {1}", this, collision.gameObject);
    }

    /// <summary>
    /// Unity method called on every frame as two objects stay collided with each other.
    /// </summary>
    /// <param name="collision">Collision.</param>
    private void OnCollisionStay(Collision collision)
    {
        Debug.LogFormat("{0} collision stay: {1}", this, collision.gameObject);
    }

    /// <summary>
    /// Unity method called when two objects stop colliding with each other.
    /// </summary>
    /// <param name="collision">Collision.</param>
    private void OnCollisionExit(Collision collision)
    {
        Debug.LogFormat("{0} collision exit: {1}", this, collision.gameObject);
    }
}

After that, let me know what you found out in the comment section below! πŸ˜‰

And if you want to get notified on future content, sign up for the newsletter!

And I hope to see you next time! πŸ€“

4.2 5 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x