Advertisement

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

Started by March 11, 2015 04:07 PM
8 comments, last by bratiefanut 9 years, 9 months ago

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

"Don't gain the world and lose your soul. Wisdom is better than silver or gold." - Bob Marley

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.

Advertisement

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.

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

"Don't gain the world and lose your soul. Wisdom is better than silver or gold." - Bob Marley

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. smile.png

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.

"Don't gain the world and lose your soul. Wisdom is better than silver or gold." - Bob Marley

Advertisement

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

@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

"Don't gain the world and lose your soul. Wisdom is better than silver or gold." - Bob Marley

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.

My problem was in my code too. I didn't checked when the objects colliding at the same time.

"Don't gain the world and lose your soul. Wisdom is better than silver or gold." - Bob Marley

This topic is closed to new replies.

Advertisement