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);
if (col.gameObject.name == "mask") {
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