CTIN 583
Week 3 Wednesday
Today
-
Collisions
-
Demo: Pachinko
-
Lab: Interaction breakdown
Collisions
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:
- "Regular" collisions
- Trigger collisions
- 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 detects these collisions, but it ignores them altogether. It will not pop the objects apart, and no scripts will hear about it.
Stretch Break
Demo: Pachinko
Setting up Collisions
- Define and describe the behavior you want.
- Add components to the objects.
- Add a script to one of the objects.
- Add a collision method to the script.
- Add a Debug.Log statement to the method.
- Test the collision.
- Write your scripts to do what you want.
Describe Behavior
Imagine and describe the behavior you want.
- Which objects will collide?
- Is it a soft or hard collision? 2D or 3D?
- What should happen when these objects collide?
- Should it happen more than once, if the collision happens again?
Set up Components
Add components to the objects and change their settings as needed.
- Two colliders, one Rigidbody. Use 2D components for 2D collisions.
- Set the isTrigger property on the colliders.
- Adjust gravity on the Rigidbody if needed.
Attach Script
Make a new script and attach it to one of the colliding objects. Or open a script that is attached to one of the objects.
There are no hard and fast rules about which objects should handle the collision. But you probably don't want a Player class handling sixteen kinds of collisions.
Add Method
Add a collision method to your attached script. You will almost certainly use one of these:
OnCollisionEnter OnTriggerEnter
OnCollisionEnter2D OnTriggerEnter2D
Set up Test
Type a Debug.Log statement into your collision method. I suggest this:
Debug.Log("Collision between "
+ gameObject.name
+ " and "
+ collision.gameObject.name);
(Yes, you can add line breaks in most lines of C# code.)
Test
Run your game.
Note that you can drag one object on top of another in the Scene view to make them collide.
Check that you see your debug message when you expect to.
Write your script
Now that you know the collisions are happening, you are on firm ground.
You can go ahead and write the code that actually handles the collision (playing a sound, etc.)
But I suggest commenting out the Debug statement rather than deleting it.
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:
Lab: Think Like a Programmer
Here are the instructions for this lab.
Candy Crush Interaction Breakdown
Here is my first attempt at the exercise.
It is certainly missing some things.
Candy Crush Interaction Breakdown
CTIN 583 - W3 Wed Jan 26 - 2022 Spring
By Margaret Moser
CTIN 583 - W3 Wed Jan 26 - 2022 Spring
Collisions. Thinking like a programmer.
- 263