Reputation:
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);
}
}
Upvotes: 4
Views: 5508
Reputation: 125245
To sum it up, I want to find both colliders involved in a collision
There is a gameObject
variable declared when your script inherits from MonoBehaviour
. This variable refers to the GameObject this script is attached to. You can get one GameObject with that gameObject
variable and the other one from the Collider2D
argument in the OnTriggerEnter
function.
void OnTriggerEnter2D(Collider2D collision)
{
GameObject obj1 = this.gameObject;
GameObject obj2 = collision.gameObject;
Debug.Log("Triggered Obj1: :" + obj1.name);
Debug.Log("Triggered obj2: :" + obj2.name);
}
EDIT:
The objects are useless to me. I need the colliders. And no, I can't use 'getcomponent' because they have more than one collider, and I need only the ones in the collision
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 named theOtherCollider
to store the collision data first reported when OnTriggerEnter2D
is called then have a another boolean
variable named detectedBefore
to determine if the OnTriggerEnter2D
has been called before.
When OnTriggerEnter2D
is called, check if the local/this version of that boolean
variable is false
. If it's not true
then this is the first time OnTriggerEnter2D
has been called. Use GetComponent
to get the other script then set the boolean
variable of the other script to true
. At the-same time, also initialize the theOtherCollider
variable on the other script with the Collider2D
value from the OnTriggerEnter2D
function.
Now, if OnTriggerEnter2D
is called and the local/this version of that boolean
variable is true
, set it to false
to reset it then obtain both colliders with theOtherCollider
variable and the Collider2D
variable from the OnTriggerEnter2D
function.
This may be confusing but by looking the code, it is easier to understand.
Note:
YOURSCRIPT
is the name of the script the OnTriggerEnter2D
function is inside and that is attached to the Colliders. You must change it to whatever that script is named.
public bool detectedBefore = false;
public Collider2D theOtherCollider;
void OnTriggerEnter2D(Collider2D collision)
{
//Get both colliders then exit if we have already ran this code below
if (detectedBefore)
{
//Reset
detectedBefore = false;
//Get both Colliders once below
Collider2D col1 = theOtherCollider;
Collider2D col2 = collision;
Debug.Log("Triggered Obj1: " + col1.name);
Debug.Log("Triggered obj2: " + col2.name);
return; //EXIT the function
}
//Set the other detectedBefore variable to true then set get the first Collider
YOURSCRIPT myScript = collision.gameObject.GetComponent<YOURSCRIPT>();
if (myScript)
{
myScript.detectedBefore = true;
myScript.theOtherCollider = collision;
}
}
Upvotes: 3
Reputation: 3108
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.
Upvotes: 0