Unity Collision Detection | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Unity Collision Detection

I would ask this on the Unity Forums but you guys here are much better to ask :). I have a game in unity, and the collisions are working all fine, except for one of them. The game is like snake. The snake doesn't die when it hits it's own tail, in fact, the collision isn't even detected. But the snake can eat food perfectly well. I'm really stumped. What is wrong? Thanks in advance!

26th Dec 2018, 5:33 PM
Paul Grasser
Paul Grasser - avatar
4 Answers
+ 2
we cannot see your "c#" code.debugging your problem without seeing the code will be impossible. if your game contains multiple files,i suggest you upload the one involving the collisions or whichever script you suspect the problem is coming from to sololearn. hola
26th Dec 2018, 6:25 PM
᠌᠌Brains[Abidemi]
᠌᠌Brains[Abidemi] - avatar
+ 1
Paul Grasser Yes that should be fine. I just didn't know if there was a rigidbody involved or not. Add a print statement at the top of the method onCollisionEnter. See if it outputs anything. I know you said the collisions aren't being detected but by this do you mean the method isn't being called or your code is not fully running? If the print statement runs, its a runtime bug with your code. If it does not run, it's likely some sort of problem with the components. Other things to try: * Is there a gravity scale on the snakes head's rigidbody? (If yes, set it to 0). *assure 'is kinematic' is unchecked on the snakes head * assure both snakes body and head collidors are 2d. Are they slightly touching when the program initially starts? Also make sure the rigidbody is 2d. * Try setting the rigidbody of the head's collision detection to continuous
27th Dec 2018, 2:58 PM
Rrestoring faith
Rrestoring faith - avatar
0
Not alot of info given but, here's some possible reasons for the problems: - Do the snakes parts contain a rigidbody? If you are using unity's onCollisionEnter methods: Collisions will not be detected if there isnt atleast one rigidbody. It's possible the food has one, but not the snake. Which would cause those issues. *note that this rigidbody cannot be non kinematic* - If you are using a mesh collidor on the snake, try making it convex If the snake uses a mesh collidor, it won't detect a collision with another mesh if you used onCollisionEnter without it being convex. - Is the snakes collidor a trigger? In that case, you cannot use onCollisionEnter with a trigger on trigger collision.
27th Dec 2018, 5:39 AM
Rrestoring faith
Rrestoring faith - avatar
0
Some more info: The head of the snake has a rigidbody. The food does too. Just the rest of the snake doesn't. If i out a rigidbody on them, it all looks really weird as they push each other to the side. But if the head has one, shouldn't that be enough? I am using unity's OnCollisionEnter2D for food and rest of snake.
27th Dec 2018, 7:03 AM
Paul Grasser
Paul Grasser - avatar