# Unity: Prerequisites for functional 2D collision detection?

## Recommended Posts

Hey everyone, happy Friday!

Sorry this is probably in the wrong place - I didn't see any clear-cut way to make this for Unity (only option was Unreal, which I like but has nothing to do with my question lol).  I've been ramming my head against the wall at 88 miles per hour trying to get to the future where I understand 2D collision detection in Unity (see what I did there? ).

So basically, I've been trying to get a sound to play on collision, and failing miserably.  After reviewing the API documentation dozens of times over, it occurred to me that the issue was not my audio code but the collision detection part.  Here's my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class bounce : MonoBehaviour
{
public AudioClip[] clips;
private AudioSource Audio;

void Start()
{
Audio = GetComponent<AudioSource>();
}

void OnCollisionEnter(Collision other)
{
if (other.gameObject.name == "top_boards")
{
Debug.Log ("Hey McFly you bojo!  Those boards don't work on water!");
Audio.clip = clips [0];
Audio.Play();
}
}
}

In the GUI, there are 2 sounds in "clips" and they both contain valid audio assets (".ogg" files, tested to confirm they work by temporarily setting "play on awake" to true).  And audio stuff aside, just the Debug.Log should have worked.  The game is basically Pong, and every scrap of info I found on Google basically told me to do what I did - but the event never seems to happen, because {reasons}.  Then I found this: http://answers.unity3d.com/questions/657234/2d-object-collision.html

Following this guy's suggestion, I made everything in my game a "Trigger" (whatever that means) and changed the event to " OnTriggerEnter", and suddenly the event started firing, because {why not?}.  Of course the objects no longer MOVED on collision, so I kept fighting this stupid thing till I lost a few more locks of hair.

Clearly, there are other aspects of this that I just haven't learned yet, some mysterious prerequisites that need to be done from the GUI before the code will run as everybody says it will, nobody seems to bother with explaining in their tutorials.  Cryptic lingo like "Triggers" and "Kinematic" are as nonsensical as "Bojo" without some general context.  Does "trigger" mean the object is able to trigger collision events?    I didn't study quantum mechanics in high school, so noob-friendly English is preferrable (I'm no noob to C#, but the math behind physics is basically lost on me).  Thanks!

##### Share on other sites

Getting 2D collisions to fire requires the following:

• Both of the colliding objects must have a 2D collider.
• At least one of the colliding objects must have a 2D rigid body component.

In my 2D project the rigid body component has the Simulated checkbox selected. I'm not sure if that's a requirement.

To answer your question about triggers, triggers are used to fire gameplay events. Imagine you have a role-playing game and you have a trap that fires arrows at the player when the player steps on it. To make the trap fire, you would give the trap a collider and make the collider a trigger. The trap's trigger handling code would fire the arrows.

Edited by szymczyk

##### Share on other sites

Awesome!  Thanks for explaining all this.  I've often wondered what "rigid body" meant too, but all the YouTube vids and forum posts say to create one; now that I know it's needed for collision checking it makes a lot more sense.

And the trigger thing sounds really cool!  I could see it being useful not just traps, but all kinds of stuff; kind of like in Legend of Zelda, Link to the Past, where you step on these yellow circles in dungeons to make treasure chests appear, doors unlock etc.  It could even help in the simple game that got me writing this post (air hockey) for things like when the puck goes into the net.

##### Share on other sites

To go more in depth - A rigid body is nothing more than an object that can react to physics, collision, or otherwise affect other said objects. When something is "kinematic" aka. Rigidbody.isKinematic = true; then the object is no longer affected by physics or collision, and instead must be controlled via script.

For example, instead of letting physics handle the bounce off an object when one collides with another, you can get the collision point of the objects from the collision event, as well as the location of the other object and then tell the colliding object to ricochet in some random direction away from the object adding a it of chaos. This kind of effect would only work if the object was kinematic as you would apply the force via script onCollision rather than letting physics handle it.

To go more in depth on Triggers - a trigger in Unity is a flag that allows an object to act as a capture event in a manner of speaking. If Collider.isTrigger = true; then the rigidbody becomes a trigger and fires OnTriggeEnter, OnTriggerExit, and OnTriggerStay events instead of registering a collision. In many cases, you'd make an object Kinematic and a trigger, so that it captures and fires a trigger event for any other rigidbody entering it. The difference between a non trigger is that, if two objects collide, a collision wouldn't be fired because of the trigger event. A good example would be a laser beam that fires through multiple enemy ships at once when fired. Rather than being affected by the enemy ships rigidbody's, the beam ignores the collisions and instead fires an OnTriggerEnter/Exit for each of the ships.

There are many uses for each and really it depends on the game. If you have any questions about this or any other Unity topic, send me a message.

## Create an account

Register a new account

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
14
5. 5

• 14
• 9
• 23
• 9
• 32
• ### Forum Statistics

• Total Topics
632619
• Total Posts
3007496
• ### Who's Online (See full list)

There are no registered users currently online

×