Week 3 Tuesday
Collisions
in the Demos/Labs folder on the shared drive
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.
There are three types of collisions in Unity:
As long as a Rigidbody is involved, Unity will pick up the collision and tell both scripts about it via the OnCollision methods.
As long as a Rigidbody is involved, Unity will pick up the collision and tell both scripts about it via the OnCollision methods.
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.
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.
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.
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.
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.
Issues with component setup:
Is the collision actually happening?
Issues with collision functions:
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: