Main

Detecting Collisions (OnCollisionEnter) - Unity Official Tutorials

Watch this video in context on Unity's learning pages here - http://unity3d.com/learn/tutorials/modules/beginner/physics/on-collision-enter How to detect collisions between objects in code using the OnCollisionEnter function. Help us caption & translate this video! http://amara.org/v/V6bh/

Unity

10 years ago

Detecting a collision in Unity requires one of the two colliding objects to contain a script with an OnCollisionEnter function in. The basic syntax takes a single argument of type Collision. >From this you can query the collision to find out information about the object that has been collided with. In this example our samoflange ball is bouncing around with a bouncy physic material and hitting power cubes. The script attached to the ball looks like this. Currently we have nothing inside the func
tion. We can make use of the collision argument to check for when it has hit a power cube. We simply use an IF statement and check 'col', our variable, for the data it contains. One example of which is the game object that it's collided with and as a result, the name. We would say if(col.gameobject.name) is equal to the hierarchical name of the power cube which is 'prop_powercube' and now this IF statement will only activate when our ball collides with our power cube. If it does collide, we'll m
ake use of the Destroy command. You can check what types of data you can retrieve from the collision by typing the name of the variable and then typing a dot (period) in the script editor. You'll then see a list of the various different data you can retrieve from the collision. Also remember to check the script reference page linked below on the collision class to see other data that you can retrieve. For now we'll simply destroy the game object. And when we save our script you'll see that whene
ver a collision occurs our ball destroys a power cube. Subtitles by the Amara.org community

Comments

@harshvardhan9032

Unity got your back even after 7 years. <3

@fatihbulut9567

If you are making a 2D game, you should use this one. void OnCollisionEnter2D(Collision2D col)

@metamango500

man, I like someone that keeps things simple and no-nonsense.

@kind_tree1868

Thanks! Tip: if anyone is making a platformer and have 2 types of coins/objects that are meant to be picked up by the player/user than you can move this code (oncollisionenter) above the code for the first object OR ELSE it may make the jumping of your object worse.

@oscarozuga

If this isn't working for you, you may want to try to make the object that is being collided (in this case the prop_powerCube) a prefab. It wasn't working in my game and after some time of trying different possible solutions, I made the object a prefab and it started working.

@theemeraldfalcon9184

me: ok modified my script lemme playtest it Unity: nah how 'bout wait 5 minutes m'kay?

@Blu3W4r10Ck

Note: This ONLY works if the script is attached to a parent object. Child objects don't seem to want to detect "OnCollisionEnter".

@skiesquiggles7319

Exactly what I needed!

@pa7ri0t

exactly what I was looking for... thnx Unity

@ELPIRATATUQUE

You just solved my problem!!! you are the Man!!! Thank you

@needfulsamurai8630

What if i want to set it not to the hierachical name "power_cube" but to anything on a special layer? Is it the same then? Thanks for any help! ^^

@diogosantos7824

For those who're having problems, here's a tip. Try to change the script name. Don't use the word 'collision' . Rename it to something like 'scriptTest' and try again!

@yongamer

I see many have problems with this. It may be because you are creating a 2D project, then you should find another tutorial. Else, it may be because you miss one of the following: collision detector for obj1, collision detector for obj2 or rigidbody for obj1/obj2.

@ralmslb

Really cool this tutorials. Just make me want to grab Unity and make a game =) Thank you and keep on doing them =) Just wish there was some sort of starting from ground 0 into a simple game such as the stealth one.

@duqer3388

GREAT TUTORIAL, SAVED MY LIFE, SALAM ALEJKUM

@xarek8694

Thanks for this lesson!

@glenperry1919

I am new to unity and do not understand it. Have you any tips or good youtube videos I could know of to improve my basic skills. Creating land and terrain, adding people, guns and making them move etc. Please help, anything useful will do.

@thegreatwarrior1239

Thank you i was stuck. I was trying to create a point system based on what the object touched. :)

@EnilionVeno

Still very helpfull vid, i got to pass one more problem on my beginners way, plus, understood some more things about code structure.

@weirdosheep619

The hierarchical name of my object is "Uncle Spleen" which does not trigger the function for some reason. Could it be reading the name wrong because of the space?