- Home /
How to Check if you are Colliding with more than one object with the same tag?
I have a problem with collision in my project. I am using a weird flappy bird model I made. It has a beak, 2 eyes, and 2 black pupils. All of those are marked with the same tag in my project. I am also using a death counter and whenever the front of the model hits something that'll kill you, The death counter goes up by 3-5 times. So I want to check that if there is more than one of the same tag colliding with a dangerous object, It will add only one to the death counter.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class FailLevel : MonoBehaviour
{
public string strtag;
private void OnCollisionEnter(Collision collision)
{
if (collision.collider.tag == strtag)
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
DeathCounter.NumOfDeaths += 1;
}
}
Answer by TheGameLearner · Jun 11, 2019 at 06:07 AM
Add a large Collider and keep it simple.
However, if you have the need for multiple colliders, Use the detection in Dangerous Object and ask it to send a count to the bird. This way, you can avoid problems.
If this too is something you can not do, or do not wish to do, then use Points array in the collision to detect each collision point and check the dangerous objects the points and other collider belongs to. This approach though a little confusing can give you perfect results.
having multiple little colliders with only one rigidbody is the same as just one big one
The script I posted is used by the dangerous object. And how do you add a large collider? I tried removing all the parts' colliders and put a mesh collider on the empty object which contains all the parts of the bird and It keeps falling to the ground. (Sorry for being Naive btw, I'm new to this)
Wait Never$$anonymous$$d. I finally figured it out. I just had to remove all the colliders of all the parts except for the body and it finally works. Thank you
Answer by xxmariofer · Jun 10, 2019 at 10:23 AM
remove the rigidbodys for the different parts, the bird should only have 1 rigibdody
None of the parts have a rigid body except for the empty object where all the parts belong to
then is imposible to receive multiple oncollisionenter messages, since if the parent has a rigidbody and the childs doesnt it will be consider one big rigidbody, and you would only receive the message once, what i would suggest you is trying to debug.log the collision.transform.name so you make sure what body is colliding, and adding the event oncollisionexit to make sure the body is not exiting hte collider
I tried using the debug.log thing. And It showed me 5 messages. Left Pupil, Left Eye, Body, Right Pupil, Right Eye
Never$$anonymous$$d, I figured it out. I just had to remove all the colliders of the bird except for the body itself and it works
Your answer
Follow this Question
Related Questions
3D Raycast Corner Collision 0 Answers
Two different spheres attached to one object for collisions 0 Answers
Character Controller collisions on older version no longer working? 1 Answer
How to Have Two Child Objects of One Parent Detect Collisions Between Eachother. 1 Answer
Is there a way to check for models stuck inside models?(see attached images) 0 Answers