CTIN 483

Week 3 Tuesday

Today

  • Collisions

The Average Everyday Adventures of Samantha Browne

 

Lemonsucker Games (Andrea Ayres Deets), 2016

Simple Game

Collisions

Lab: Collisions

in the Demos/Labs folder on the shared drive

Collisions

We've already seen collisions, where Unity won't allow two objects to be in the same place at the same time.

 

We want to know how to listen for them in scripts. But there's a bit to understand first about the types of collisions that Unity detects.

Collision Types

There are three types of collisions in Unity:

  1. "Regular" collisions
     
  2. Trigger collisions
     
  3. Static collisions

Collisions: Regular

As long as a Rigidbody is involved, Unity will pick up the collision and tell both scripts about it via the OnCollision methods.

Collisions: Regular

As long as a Rigidbody is involved, Unity will pick up the collision and tell both scripts about it via the OnCollision methods.

Collisions: Trigger

If either side of the collision has "isTrigger" checked on its collider, then it's a trigger collision and Unity will tell us about it via the OnTrigger methods.

Collisions: Trigger

Collisions: Trigger

Unity will tell us about a trigger collision via the OnTrigger methods.

 

But it won't resolve the collision – things can overlap with trigger colliders.

Collisions: Static

Some collisions just shouldn't happen, and Unity won't even tell us about them: when two colliders overlap but there's no Rigidbody involved.

This collision "shouldn't" happen because you're not supposed to move a static collider.

Collisions: Static

Unity will resolve the collision, that is, the colliders will get pushed back until they don't overlap. But no scripts will hear about it.

Stretch Break

Demo: Pachinko

Collisions: Oops

There are a whole lot of steps in setting up collisions, and therefore a lot of places you could mess up in the process.

If you observe when you mess up, you'll start to get a sense of where you're most likely to have a problem.

But a list of common problems is below.

Collisions: Oops

  • You don't have a Rigidbody and two colliders on the GOs you want to collide.
  • IsTrigger is checked when it shouldn't be or vice versa.
  • You've accidentally checked isKinematic on a Rigidbody.
  • You've used a 3D collider when you wanted a 2D collider, or vice versa.
  • You forgot to attach your script.

Issues with component setup:

Collisions: Oops

  • Your 2D colliders have different positions on the Z axis. (Generally everything should be at zero).
  • You're trying to play your game to cause the collision and you missed.

Is the collision actually happening?

Collisions: Oops

  • You forgot to add 2D in a 2D project.
  • You misspelled the function name, e.g. onCollisionEnter.
  • You're using OnTrigger functions for regular collisions, or vice versa.

Issues with collision functions:

Collisions: Learn More

There are other properties, like isKinematic, that influence whether a collision happens and which methods are called.

 

Consult the following elaborate diagram in the Unity documentation for details:

docs.unity3d.com/Manual/CollidersOverview.html

Made with Slides.com