- Home /
Simple Collision Detection not working. [Solved]
Pretty much it comes down to collision in my game doesnt seem to be working. In my game I have the character moving constantly to the right, as he passes through a cube with its Mesh renderer off I just want it to print "hit" ( at the moment anyway, my plan was for somthing else but since collision wasnt working I just want the debug.log to work).
Both objects have colliders. (tried is trigger on/off on both or either)
Both objects have Rigidbodies (tried without rigidbodies on both or either one and tried is kinematic on both or either)
I have tried OnCollisionEnter and OnTriggerEnter using the exact C# scripts.
The guy I'm working with has this code that works for collision in his level.
void OnCollisionEnter ( Collision myCollision ){
rigidbody.AddForce(0,force,0);
}
However when I copypaste it into the script tied with my objects, it doesnt work. I've made sure my objects are exactly like the ones he is using in his level.
I've read around 10 questions on this site about collision, tried their answers and still nothing. With the limited unity knowledge I have I would assume somehow ive changed some settings or somthing without knowing it. Since I can load up my friends level and the EXACT collision script works. Any help would be greatly appreciated.
Answer by Tavis · Sep 26, 2012 at 11:57 AM
After I posted this I read through some more previous questions and found one that linked this
http://docs.unity3d.com/Documentation/Components/class-MeshCollider.html
The table at the bottom is beyond helpful, I simply must have tried the wrong combination every time :(
Hi can you please provide the actual solution? I am having the same problem! I'm still learning how to use C#/Unity so the api still confuses me a little bit.
Answer by Riazu · Jan 12, 2016 at 10:05 AM
Hi there, You need to check the object if your player is colliding with the specific object or not.
For that you need tags or names or something specific. For Example, I am making a shooting game and I want to check if my bullet has collided with the enemy's collider or not so I will do it this way,
void OnCollisionEnter(collision col) { if (col.game.tag == "Enemy") { //Kill the player or play its death animation or replace it with its ragdoll. } }
In short, you need to check whether the object is colliding with that specific object or not. If it is not clear, please leave a comment... I'll try my best to explain in more detail.
Hi Riazu,
i'm a beginner Unity users and my "game" should be very simple. I would like to grab objects without collider with the shelfs, couch etc...so, how could i do? I'm using the same version of Unity.
Thanks
Answer by unity_ngP8_Fl6_Peiuw · Nov 27, 2018 at 10:18 PM
Check the layer that the offending object is on. I had the same sort of 'it isn't colliding' problem and none of the discussion mentioned layers. The object I was having problems with was assigned to the 'default' layer and everything else was on 'environment' or 'terrain' or 'player' and it wasn't until I asked the question about what layers are used for that I found out that in the Edit->project->physics menu there is a table of layers and you can select those that do or do not collide with each other. See https://docs.unity3d.com/Manual/LayerBasedCollision.html
Your answer
Follow this Question
Related Questions
Collision issue with SetParent (C#) 1 Answer
Jump on the ground 2 Answers
On Trigger Enter, Collide with object, specific collision 1 Answer
Multiple Cars not working 1 Answer
how to stop my scene on collision.. 1 Answer