- Home /
How can I differentiate between colliding with an object or its child?
When I fire an arrow (red arrows) into another other object (blue cubes) it checks the object's tag via raycast and then parents itself to that object if the tag matches (left cube). The problem is that after the parenting, the object treats the arrows as a part of itself (even though the arrows are tagged differently), and so if an arrow hits another parented arrow they keep sticking together really stupidly in an ever growing row (middle cube).
Is there a way I can ignore the parented arrows when doing the raycast so that the new arrows are only parented to the object (blue cube) and not to the arrows already parented (right cube)?
I am not sure if I understood you correctly, but what I would suggest is to disable the collision of the arrows by layering them and then disable the physics between them.
So give the arrow a layer and then go to Edit -> Project Settings -> Physics and then uncheck the check mark of the layer.
If you don't $$anonymous$$d them passing through each other, you could just turn off the collider on the arrow after parenting.
Ok, so basically the collision detection (the raycasting) of each arrow is only turned on when an arrow is actually being fired through the air. Once an arrow hits something it no longer checks for collision since the raycast is turned of. Also the arrows don't actually have colliders since the raycast does all the collision detection work.
I think the bulk of the problem comes from the fact that when an object becomes a child of something, the parent's tag in effect is transferred to the children, even if in the inspector the children have a different tag set manually. At least that is the way it is working for me. Both the cubes and the arrows have different tags, yet when the arrows become children of the cubes they behave as though their tag is the same as the cube. Yet when not parented if I fire an arrow towards a stationary arrow it will pass straight through it. Strange
Your reported behavior (getting the tag from the parent ins$$anonymous$$d of the child) sounds strange. So I decided to test it. I could not reproduce the problem. I was able to parent the "arrows" to the wall, and when I shot the arrows, I was able to detect collisions with both the wall and the arrows. So something strange is going on here. It might be a bug in your code, or there might be something different in your setup from my setup.
I'd like you to try something:
Remove the parenting code from your arrow
Go to Edit/Project Settings/Time, and change your Fixed Timestep to 0.01 or even 0.005.
Select your arrow prefab and change the Collision Detection to 'Continuous Dynamic.
Then run and see what happens.
Answer by Hamesh81 · May 25, 2013 at 12:23 PM
Ah, I worked it out. There seems to be a difference between having the children "Untagged" and actually tagged differently from the parent. I thought that since the children weren't tagged they would be detected as different to the parent which had a tag set. When I set every arrow with an actual tag (different to the parent of course) the arrows aren't sticking together anymore.
So having the children simply untagged doesn't do it, because I suppose they assume the tag of the parent. They need be tagged but tagged differently. Thanks for your help robertbu
I'm glad you got it worked out. Nice to know about untagged objects.
That's the first time I've got kudos for answering my own question!
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
How can I get the tag of a child object? 1 Answer
Access Parent NameTag? 1 Answer
GetComponentInChildren with tag in two variable 3 Answers
Get a raycast to hit only child objects to use axes 1 Answer