Detecting Collisions (OnCollisionEnter) - Unity Official Tutorials

preview_player
Показать описание
Watch this video in context on Unity's learning pages here -

How to detect collisions between objects in code using the OnCollisionEnter function.

Help us caption & translate this video!

Рекомендации по теме
Комментарии
Автор

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

fatihbulut
Автор

Unity got your back even after 7 years. <3

harshvardhan
Автор

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.

kind_tree
Автор

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.

oscarozuga
Автор

Doesn't work. I put a variable in the statement instead though so is this the reason?

rise
Автор

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

BluWrCk
Автор

me: ok modified my script lemme playtest it


Unity: nah how 'bout wait 5 minutes m'kay?

theemeraldfalcon
Автор

i did the same but i made the cube to stop from the speed of 15 to 0 but the cube is touching the obstacle and coming back like a spring for some seconds the its stopping sometimes its not stopping

muhammadzaid
Автор

exactly what I was looking for... thnx Unity

parit
Автор

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

metamango
Автор

Why I can't put another condition ? (like moving forward with "transform.Translate(0, 0, normalSpeed * Time.deltaTime);" )

nepsis
Автор

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! ^^

needfulsamurai
Автор

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

ELPIRATATUQUE
Автор

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?

weirdosheep
Автор

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.

yongamer
Автор

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

thegreatwarrior
Автор

doesn't work... I try to pick up a medicbox, and it just walks on top of it, the medicbox has the exact same name as I am referring to and they both have a rigidbody( don't know if that's a must), Please help, I need to fix this for a school assignment... 

BrentVIDSZ
Автор

Hello guys, i want to freeze player's Y position onCollisionEnter, how can i write it? Please help me

davidgoldmouthson
Автор

Why on some functions do you have to use two equals instead of 1?

microphonebeam
Автор

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.

glenperry