- Home /
2D Collision not working! (child sprites colliders)
Hello!
This is my first problem that I cannot solve without making a thread, it is just very painful dealing with this..
So the problem is that I have a parent GameObject that is my main character. That gameobject is empty, and all of his sprite parts (arms, head, feet, etc.) is made childobjects of the main character. Because they all have individual movement (in animations) and collision, and I want my parent GameObject to handle animation, player controlling and etc.
The problem is that I have tried using 2D rigidbody and a rigidbody, but they do not work; physics and collision does not work correctly. So I followed a tutorial and made "my" own, which works great, BUT, it can only have one collider. It requiers a Box Collider in the parent gameobject of all the sprites, which is very bad. I want to use all parts' individual colliders together, not one big rectangle around my main character, the animations would not work correctly and my collision would suck.
Picture:
All the small green boxes are the individual colliders of the sprites attached to "Bertil" our main character that is an empty gameobject.
This is my scripts: Scripts DL Link
$$anonymous$$ake sure that rigidbody is added to object having collider. And remember that 2D collider cann't collide with 3D collider.
Ok, thank you. I added a rigidbody 2D to the parent gameobject and the collision works now, but the gravity is not working. The character is slowly levitating upwards. How do I fix this? :) And how do I fix so that the character only collides with a certain collisionmask?
Set the "gravity scale" property of rigidbody2D to 1 then gravity will work but if gravity scale is 0 then gravity will not work. $$anonymous$$ake sure that iskinematic box of rigidbody2D is unchecked in the inspector.
I have done all that, it still doesn't work! And can I fix so that it only collides with a certain collisionmask and not everything?
Answer by Ashish Dwivedi · Feb 08, 2014 at 07:16 AM
Yes you can fix it so that it collides with a certain object. For this you need to use layer for each object and assign layer to objects and call "Physics2D.IgnoreLayerCollision(int layer1, int layer2, bool ignore = true)" this method by passing the index of layer and the bool according to what you want whether object under both the layer should collide or not. Like wise you can enable and disable collision between any number of objects or collider.
IgnoreLayerCollision reference
Thank you, the gravity and collision works now. But the collision kind of the character "stutters" when walking in to a wall or walking up a ramp. I can even if I push long enough into a wall get through it, how do I solve this?