Prefab Collider2D not Recognized as Raycast Collider

Hello everyone :slight_smile: Here’s the situation:

I have a baddy prefab which has two collider components: a simple CapsuleCollider2D used for physics, and a trigger PolygonCollider2D used only for mouse point collisions.

The PolygonCollider2D is updated through an attached script (Baddy.cs) only when needed using:

if(this.gameObject.GetComponent<PolygonCollider2D>()!=null){
	Destroy(this.gameObject.GetComponent<PolygonCollider2D>());
}
this.gameObject.AddComponent<PolygonCollider2D>();
this.gameObject.GetComponent<PolygonCollider2D>().isTrigger=true;

Yes, this is poor practice, but it get’s the job done. It works perfectly: in the scene view of the game I can pause and see that there is indeed a polygon collider on our baddy prefab.

In another attached script (CollisionHandler.cs) we check for a mouse press on our baddy:

if(MousePress()){
	hit=Physics2D.Raycast(level.mousePoint, Vector2.zero, 0f);
	if(hit&&hit.collider==this.gameObject.GetComponent<PolygonCollider2D>()){
		print("baddy mousePress");
	}
}

However hit.collider==this.gameObject.GetComponent<PolygonCollider2D>() turns out false, even though I could print both hit.collider and this.gameObject.GetComponent<PolygonCollider2D>() immediately before hand in our if(MousePress()) and have them both defined as:

D_Prefab(Clone) (UnityEngine.PolygonCollider2D)

They couldn’t be different PolygonColliders because there can only be one at a time, since our Baddy script removes and creates the PolygonCollider simultaneously.

Any ideas what could be going wrong?

First thing I can see is that your hit check reads as false if it doesn’t hit the polygon collider, but a raycast will stop at the first collider it hits, so it’ll also returns false if it hits some other collider, including your capsule collider on the same object.

Your raycast isn’t casting. Well, it is, just not very far.
Let me explain, the Physics2D Raycast takes in an origin, a direction, a distance. and a few other titbits.
You’ve written it like this:

hit=Physics2D.Raycast(level.mousePoint, Vector2.zero, 0f);

Your origin is fine, your direction and distance on the other hand are not. You haven’t specified a direction and your casting distance is 0. As a consequence, your ray won’t hit anything because it doesn’t know which direction to go and it’s length is 0, by default distance is Mathf.Infinity. So the cast can’t hit anything.

You need to decide which direction to fire in and how far, if you want to specify a distance. This should fix that issue.
A few potential issues that may arise afterwards though are detecting the right collider. If the ray hits any collider, it will immediately return that collider, regardless of which collider it is.

Probably the better, and more useful, solution is to make use of a layer mask. This approach involves moving the enemies onto a separate physics layer, so when you do the raycast, you only cast on that layer. A layermask is one of the extra titbits that you can pass to the raycast function:

hit = Physics2D.Raycast(level.mousePoint, Vector2.Right, 20, LayerMask.GetMask("MyEnemyLayer"));

That should fix any issues that you may run into. The direction, distance and layermask are up to you. Have a peak at the LayerMask documentation for info about how to use it.

Also, make sure that you setup the Physics2D Manager to query triggers, if you don’t, the raycast will fire but may ignore the triggers.

Something else that you may want to look into is the mouse down event that Monobehaviours can subscribe to. This is fired whenever the user clicks on a collider, or GUIElement. You don’t have to worry about raycasting yourself then, and you don’t have to check which collider was hit. If this event is raised on a baddy component, you clicked on a baddy.

Hopefully this helps.