Let’s Get Physic-al

Erin Kerr
3 min readOct 1, 2022

An introduction to Physics in Unity

Example of Unity’s built in physics engine at work

Unity contains a built in physics engine, allowing you tap into real-life simulations in your Unity projects without building them from scratch. Two key components from the physics engine are Rigidbodies and Colliders.

  • The Rigidbody component can be used to set the mass of an object, to change how it moves and how it is affected by other forces, like gravity.
  • The Collider component can be used to define the boundaries of an object for collision detection

Collisions in Unity open up a new world for your games, as defining how different objects interact is crucial for developing dynamic gaming experiences. To enable a collision, you’ll need two objects: both containing a Collider component and at least one containing a Rigidbody. In the example above, each ball has a Collider and a Rigidbody, where as the walls and floor of the box only have a Collider.

After that, you can tweak the Physic Material, mass, drag and more of the objects involved to change how they interact with each other physically. This type of collision is called a Surface Collision, since the surfaces of the two objects are interacting with one another.

For those who still want more control or want to add additional behaviors to the physical collisions, Unity supplies the following methods:

  • OnCollisionEnter: called when a collision starts
  • OnCollisionStay: called once per frame while a collision is occurring
  • OnCollisionExit: called when a collision ends
Reducing the green value of each ball with each call of OnCollisionEnter

Each method above gives you access to the Collision object, which is a collection of data regarding the collision, including the point of contact and information about the other object in the collision. Using these methods, you can add all sorts of other behaviors based on the collision, such as reducing how green an object’s color is each time it collides with another object (as shown above).

Now, some of you may be thinking “what if I don’t want my game to be so realistic?” Don’t worry — Unity’s got you covered. If the Surface Collision doesn’t meet your needs, try out the Trigger Collision. Same basic requirements (two objects, two Colliders, at least one Rigidbody), but you can turn off the physics interactions by checking Is Trigger? on each Collider in the Inspector.

Increasing the size of the cube with each trigger collision using OnTriggerEnter

Just like with surface collisions, Unity supplies methods to add behaviors to trigger collisions:

  • OnTriggerEnter: called when a trigger collision starts
  • OnTriggerStay: called once per frame while a trigger collision is occurring
  • OnTriggerExit: called when a trigger collision ends

The main difference between these methods and those for surface collisions is that trigger collision methods give you access to the other object’s Collider, rather than the Collision object.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response