- Home /
Script detecting collisions of child object?
Hello, I recently wrote a script for a 2D game that included jumping and it worked fine. I converted everything for 3D and made some edits so I could use 3D models for my platformer instead of sprites, however the script seems to be picking up collisions for child objects along with the main object.
The below is the part of the script that deals with collisions and is attached to the main object which is a capsule, I also have a child object that is a cube with no scripts attached however the main script is picking up collisions from the cube causing me to be able to stick to walls because my script thinks I am grounded, any ideas?
void OnCollisionStay(Collision coll){
/**
* This is for checking the tag of an object you are colliding with
* to see if it is a platform. If the tag is "Platform" then we set
* the variable isGrounded to true then break the switch, This was
* made a switch statement so more tags can be added later on.
**/
switch (coll.gameObject.tag)
{
case "Platform":
isGrounded = true;
Debug.Log("Grounded" + n);
n++;
break;
}
}
void OnCollisionExit(Collision coll){
/**
* When player stops colliding with a game objects it checks
* to see if isGrounded is true then, assuming it is, sets it to false.
**/
if(isGrounded){
isGrounded = false;
}
}
Like mattyman174 said that's how it works. You could try adding a rigidbody to the child objects and making it kinematic. $$anonymous$$aybe that will work and separate collision detection?
ya..... that caused my character to float backwards randomly, I can't believe that there is no way to disable this "feature"
If you need the cube to follow around the main object, you can also have it not be a child, and write a script for the cube to follow the main object and stay at given position away from it. I do that for having GUIText follow my objects, for example, since you can't just make GUIText a child of some object and expect it to show up on screen.
Answer by mattyman174 · Feb 27, 2014 at 11:02 PM
Collisions detected on Child Objects of the Parent is normal behavior.
Any Child Colliders are added to the Main Parent Collider and are all treated as one big Collider.
If you want to remove this functionality, remove the Colliders from the Child Objects.
Your answer
![](https://koobas.hobune.stream/wayback/20220613134915im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
How to set parent collider equal to child collider visually? 2 Answers
How to access children's colliders. 4 Answers