To sum up the following: I want to find both colliders involved in a collision, from an OnTriggerEnter2D event. How can I do this?
I have two gameobjects. Both have a collider and a trigger.
On object A, it's surrounded by the trigger. On object B, the trigger only surrounds a certain section.
When the trigger of object A touches any collider, trigger or not, of object B: I want object B to lose health. And vice versa.
However, when the trigger of object A touches the collider (not trigger) of object B, both objects lose health.
I get this in the console
Object A hit Object B
Object B hit Object A
I came to the conclusion that the trigger of object A was calling the Ontrigger2d event on object B.
I think the best way to deal with this, is to find which collider 'found' the collision, and depending on that: ignore the collision..
How can I find which trigger 'found' the collision?
[Also posted on Unity Forums]
EDIT: Code
private void OnTriggerEnter2D(Collider2D collision)
{
Consumeable con = collision.GetComponentInParent<Consumable>();
if (con != null && con.gameObject != gameObject)
{
Debug.Log(gameObject.name + " hit " + con.gameObject.name);
con.Damage(1);
}
}
There is a
gameObject
variable declared when your script inherits fromMonoBehaviour
. This variable refers to the GameObject this script is attached to. You can get one GameObject with thatgameObject
variable and the other one from theCollider2D
argument in theOnTriggerEnter
function.EDIT:
The colliders should be made child of the GameObject and the script must be attached to each child collider then what's in this answer should work.
If some reason you must do this without making the colliders child of that GameObject then use a boolean variable to detect the collision once only.
This is a modification of an answer from this post.
Have a local
Collider2D
variable namedtheOtherCollider
to store the collision data first reported whenOnTriggerEnter2D
is called then have a anotherboolean
variable nameddetectedBefore
to determine if theOnTriggerEnter2D
has been called before.When
OnTriggerEnter2D
is called, check if the local/this version of thatboolean
variable isfalse
. If it's nottrue
then this is the first timeOnTriggerEnter2D
has been called. UseGetComponent
to get the other script then set theboolean
variable of the other script totrue
. At the-same time, also initialize thetheOtherCollider
variable on the other script with theCollider2D
value from theOnTriggerEnter2D
function.Now, if
OnTriggerEnter2D
is called and the local/this version of thatboolean
variable istrue
, set it tofalse
to reset it then obtain both colliders withtheOtherCollider
variable and theCollider2D
variable from theOnTriggerEnter2D
function.This may be confusing but by looking the code, it is easier to understand.
Note:
YOURSCRIPT
is the name of the script theOnTriggerEnter2D
function is inside and that is attached to the Colliders. You must change it to whatever that script is named.One way to achive this is to create a child gameobject to handle one of the colliders.
So for example you have a parent object that has a non-trigger collider, and a child with a trigger collider.
This way its easy to figure out wich colliders are involved in the collision.