• 13
• 15
• 27
• 9
• 9

# [UNITY] Problem with 2d colliders in unity game.

This topic is 1087 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello everybody, long time no see huh? I haven't been active since few months ago and its good to be back :D

I am making a 2d  archery game and I have problems with a little bug.

I will try to explain it as accurate as a I can. So, I have a 2d background and a target. I want my target to "hide" in between the rocks and the tree and I did that with a "mask". The problem is that i don't want to be able to shoot the arrow through the tree or the rocks and hit the target. I have a polygon collider on my mask but is not allways working and i don't know why. I know it's 2D and the colliders overlap so how can I make some kind of colliding order? I am not sure if I explained very well so i will post some code and some images.

This is the OnTriggerEnter2d function code for the collision, attached to my arrow (if arrow hit the mask, destroy the arrow so it can't reach the target. But it is not working every time.):

void OnTriggerEnter2D (Collider2D col)
{
if (col == null)
Destroy (gameObject);

GetComponent<Collider2D>().enabled = false;
GetComponent<Rigidbody2D>().isKinematic = true;
Destroy(gameObject);
Debug.Log("Hit");
ok = false;
}
if (col.gameObject.name == "Target" && ok) {
GetComponent<Rigidbody2D>().isKinematic = true;
transform.parent = col.transform;
GetComponent<Collider2D>().enabled = false;

//Debug.Log (transform.localPosition);
float distance = Vector2.Distance(new Vector2(transform.localPosition.x, transform.localPosition.z), new Vector2(0, 0));

if(distance <= 0.1){
points = 5; particle = 0;
Invoke("scoreAndEffects", 0.15f);
CheckGameState.instance.hitCenter += 1;
}
else if(distance > 0.1 && distance <= 0.2){
points = 4; particle = 1;
Invoke("scoreAndEffects", 0.15f);
}
else if(distance>0.2 && distance<=0.3){
points = 3; particle = 1;
Invoke("scoreAndEffects", 0.15f);
}
else if(distance>0.3 && distance<=0.4){
points = 2; particle = 2;
Invoke("scoreAndEffects", 0.15f);
}
else if(distance>0.4 && distance<=0.6){
points = 1; particle = 2;
Invoke("scoreAndEffects", 0.15f);
}
ok=false;
//Debug.Log(distance);

}
}


[attachment=26375:2015-03-11_175626.png][attachment=26376:2015-03-11_175726.png]

And here is a link to the exe of this scene if someone wants to try out and see the bug: https://www.dropbox.com/sh/ye2dh7bcz1oc8ic/AABAFmOkWQ2J-9hJ9bBapMKpa?dl=0

##### Share on other sites

This could be because the arrow is traveling so fast it's going entirely through the rock in a single frame. It depends how fast the arrow is traveling and the size of the colliders. I'd advise making the arrow slow to eliminate that possibility first.

Two other things I noticed:

You have a 2D collider on the rock - does the arrow also have a 2d collider? I'm not sure if 2D/3D colliders are compatible.

There's a warning on the game object you've selected (in the first screenshot) about the collider, that might also be the reason.

Edited by stevenmarky

##### Share on other sites

I've had something similiar with 2D collision, it was fixed when i added rigidbody2D to the object, just make sure one of the objects are destroyed... Otherwise they won't stack, causing each other to drag off the screen. Though your case should be more problematic.

##### Share on other sites

I have a rigidbody2D on the object now and both colliders are 2D. Nothing changed.

##### Share on other sites

i googled it too back then, there were also reports of 2D collision not working while 3D has.

There're also people complaining about GetComponent element on the documentation being wrong.

i hope these help because it's all i know of the subject at hand.

##### Share on other sites

I still can't find a solution.. I used layer matrix collision and all that stuff and couldn't figure out why it's not working. I have done this project in unity 4.6 and recently upgraded it to unity 5, maybe that is a possible cause, unity 5 collision functions not working properly.

##### Share on other sites

Just wondering, have you tried using the Sorting Layers to be able to hide the target behind trees and rocks (target in a sorting layer one level lower than trees/rocks)? Just wondering if taking the mask out of the equation might help simplify things so the solution is easier to find.

Jay

##### Share on other sites

@Jay I tried and not working as I expected. Anyway, i solved the problem :D. Sadly, the problem was in my code. In the OnTriggerEnter2D function i checked the colision with the mask and then i checked the collision with the target. This was the wrong aproach because in 2D there was no Z axis and the mask and target overllaped. I solved this by explicit checkind when the arrow collided with the mask and the target at the same time and when collided with mask and not with target and so on.

Thank you everyone. :D

##### Share on other sites

For those who care; I've just fixed the issues i've had with the colliders; when something like that happens the go-to check elements would be the prefabs. If you select non-renewed prefabs (by chance when you set public variables through the scene instances) your commands are directed to the prefab instead of the in-game element, hence the colliders work but the code doesn't. Now i just need to figure out why i didn't get a null reference exception when i hit the play button.

Edited by cfreak19