Is there a way to make OnTriggerEnter work only for the colliders marked as triggers?

I’ve looked around, but I haven’t quite found my question. I’m making a very simple game and one of the aspects of the game is pushing and pulling a box. I got that part figured out, but currently if the player stands on the box, they can pull it around. I tried fixing this by making the trigger close to the ground, but, I guess, on trigger enter works for colliders that aren’t marked as triggers as well.

I feel like I’m missing something obvious. I could, probably, just put this behavior in a child object and put the trigger there. But I want to know if there’s a smarter solution.

Relevant Code:

void OnTriggerEnter2D(Collider2D other){
	if (other.tag == "Player") {
		near = true;
		//Debug.Log ("Near is:" + near);
	}
}
void OnTriggerExit2D(Collider2D other){
	if (other.tag == "Player") {
		near = false;
		//Debug.Log ("Near is:" + near);
	}
}

Screenshot of Colliders:
85394-theproblemathand.png

For people wondering, I want to mention that OnTriggerEnter calls for any trigger, whether or not it belongs to the object, so my player's trigger was calling the function. I don't know the fix for this (perhaps simply a very small trigger on either side of the box and putting the functionality on those). I don't need a trigger on my player for this game, so I simply removed it.

2 Answers

2

If you need to make it work only with triggers, then just check if the other collider is a trigger using other.isTrigger. Problem solved. For more advanced control you will need to look into collision matrix.

thanks that helped me!

OnTriggerEnter does only fire when a collider enters another marked as a trigger. It looks from your screenshot that you have both a trigger and a non-trigger collider on the same object. This makes a compound collider - effevely merging the bounds of the colliders into one.

To keep them separate, add the collider components to separate child objects and/or place them on separate collision layers.

My dude, my pal, my friendo. This is exactly what I was looking for. I figured that makin one a child object was the solution. Thanks for the info on compound colliders. Hadn't heard of that, but it explains so much!