Issue with child object collider moving through other colliders
So I'm having a bit of an issue with implementing a shield for my player. I have a shield child object on my player which has both a box collider and a rigidbody, and I have a capsule collider and a rigidbody on my parent player object as well. I'm able to detect from the enemy script whether the enemy collides with the player or shield, but as you can see, the shield's collider passes through the enemy which allows my player to collide with the enemy as well, which I obviously don't want.
When the player holds the shield button, i enable the shield's collider (as in set the gameobject to active) and don't want anything to go through the shield collider and touch the player. Removing the rigidbody on the shield fixes this issue but then collisions are only detected on the player and not the shield.
Any ideas as to why the shield collider moves through other gameobjects? (the enemy also has a capsule collider and a rigidbody and none of the colliders are set to trigger). Of course if there's a better way to implement my shield functionality all together, I'm open to that as well.
][1]
I don't see how this can be an issue with code, since collisions are being registered fine, the only issue is that the child object collider moves through other colliders while the parent object doesn't. Nonetheless here's how collisions are being detected in the enemy script. Both methods in Player and PlayerShield are being called fine.
void OnCollisionEnter(Collision collision){ if(collision.gameObject.name == "HeroKnight"){ collision.gameObject.GetComponent<Player>().TakeDamage(20); } if(collision.gameObject.name == "Shield"){ collision.gameObject.GetComponent<PlayerShield>().msg(); } }
Are you sure that it works when you remove rigidbody or is it the other way around
yes, 100% sure the shield collider works as intended (not going through the enemy collider) when I remove rigidbody.
Answer by maewionn · Nov 27, 2021 at 04:37 AM
Well, the basic question here is: What collision detection is your rigidbody set to? If it is discrete (the normal option) things will likely clip through, depending on how fast they move. Try continous dynamic, and if that doesn't work, go for continous speculative.
You did not specify what kind of shield it is. If it is an energy bubble, maybe a distance check each frame would work (if there are not too many enemies, else it would be quite costly). If you are using navmesh agents, adding an obstacle to the player would prevent enemies from entering it. If it is a physical shield, you can do a few raycast from the enemy weapon and see if any of it hit the shield and then block the movement.
Hope I could help :)
Your answer
