Unity Development — Collisions

So you’ve been following the Unity Developer Journey and have read that rigidbodies are necessary for collisions, but wanted to learn more about these collisions. No time like the present to dive right in.

Which way are they going to collide?

There are two types of collisions in Unity that we are going to talk about. These are regular collisions and triggers. A regular collision is exactly what you imagine it would be. A car hits another car, so it skids in the direction of the force. Someone pushes someone else and they fall. That is a regular collision.

A collision of two cubes

A trigger is when there is no physical force applied, but an event occurs because they touched. While these don’t really exist in real life, some perfect examples in video games include collectibles (Mario collecting coins and Sonic grabbing rings) or other pickups (Walking over rupees in The Legend of Zelda and apples in Crash Bandicoot).

One cube passing through another and a ‘trigger’

Besides a Rigidbody, the other thing you need to perform these collisions is a collider component. Usually this is determined by the shape of the object (box collider, capsule collider, etc). They all have the option of whether or not to be a trigger. This would mean that when another object hits they’re collider, it would not act as if hitting a physical object. It would simply go through. With these two distinctions, we can actually get into the code as well.

A standard Box Collider with the ‘Is Trigger’ option ticked

On the object with the Rigidbody and collider, you can add OnCollisionEnter() or OnTriggerEnter() to their script. Try to remember the examples given to determine which is the proper way to go for your situation.

The “other” in both cases is the object that has collided or triggered this object

If you want an enemy missile to knock your vehicle with physical force, use OnCollisionEnter(). If you simply want an enemy projectile to take away some health, use OnTriggerEnter(). In the code, you can customize numerous possibilities for all your game design thoughts.




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The Tools We Use To Stay Afloat

Screenshot of one example Sprint board at Colloq, reflecting the described features below

Export MySQL data to CSV with phpMyAdmin

GO-GRPC [Guide for inter-service communication]

How VERY 2020 of you!’

Building a Quick and Cheap Nas with TRUENAS.

Observability and GitOps

Five Characteristics of the Best Project Management Methodology

Demystifying Streams in Nodejs

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Christopher Graf

Christopher Graf

More from Medium

Player 1 Is Ready To Move Around

More than a Plane: Environment

Cinematography in Unity — Skyboxes

Racing Car Game # Day 3 — Steering