- Home /
Dumb mistake on my part, had an additional collider on the player object that was properly detecting collision on the player object.
Detecting collision on an object and not its sister object.
Forgive me if this isn't really the proper way to describe this, but I'll try to clarify.
I have a parent GameObject that I'll refer to as the "capsule" object. It has two child GameObjects that each have their own Rigidbody and their own Trigger Box Collider. One child object has a script on it (it is the player script that handles movement and other player functions) and the other child object (referring to its sister in the title) is essentially a front bumper that extrudes out in front of the player object. When I call OnTriggerEnter in the player script it gets the Box Collider on the bumper object, which isn't what I want in this case. Is there a way to make sure OnTriggerEnter isn't firing off when its sister object is the one getting collision or should I be using a different function for this purpose?
Structure:
Player:
The bumper just has 3 components: Transform, Box Collider (trigger enabled), and Rigidbody.
Collider code on the player script:
void OnTriggerEnter(Collider other) {
//If a player walks into the powerup, grant the player the powerup
if (other.gameObject.tag == "Powerup") {
//etc
}
}
Could you verify your script, or provide the necessary part of it here, as the box collider of one gameObject doesn't trigger any event for any other object. In fact I have been working to get the behavior that your are experiencing for a project of $$anonymous$$e, and just completed it a few hours ago, so I can confirm this is not the default behavior. Also, please confirm that the scripts have been added to the correct object.
Answer by Shemamforash · Jul 20, 2018 at 04:18 PM
Hi there! Sounds like you want to use collision layers. In the Physics window you can see which collision layers interact with each other in a handy matrix. First you'll want to create two new layers, Bumper and Player (for example) and set the layer of the Bumper and Player objects accordingly. Then in the collision matrix in the Physics window, uncheck the box where the Bumper and Player row/column intersects. Now when you hit play, you'll find that OnTriggerEnter is no longer called, as the Physics engine is ignoring collisions between these two layers! Let me know if this helps at all :)
Alright, well I realized I made a silly mistake and accidentally had an additional collider on my player object, but this is useful information nonetheless, thanks!