OnTrigger to OnCollision

Hello,

I’m trying to do what is being shown in this video (OnTrigger):

The problem with OnTrigger is that all objects will go through and i only want the box to be affected so i want to use Physics.ignoreCollision instead of isTrigger.

This is the code i use for OnTrigger:

public Color color01;
public Color color02;

void OnTriggerEnter (Collider other) {
		tk2dSprite otherSprite = other.gameObject.GetComponent<tk2dSprite>();

		if(otherSprite.color == color01){
			otherSprite.color = color02;
			transform.collider.isTrigger = true;
		}else if(otherSprite.color == color02){
			otherSprite.color = color01;	
			transform.collider.isTrigger = true;
		}else{
			transform.collider.isTrigger = false;	
		}
	}
	
	void OnCollisionExit (Collision other){
		transform.collider.isTrigger = true;	
	}

and this is the code i want to use instead:

public Color color01;
public Color color02;

void OnCollisionEnter (Collision other) {
		tk2dSprite otherSprite = other.gameObject.GetComponent<tk2dSprite>();

		if(otherSprite.color == color01){
			otherSprite.color = color02;
			Physics.IgnoreCollision(transform.collider, other.gameObject.collider, true);
		}else if(otherSprite.color == color02){
			otherSprite.color = color01;	
			Physics.IgnoreCollision(transform.collider, other.gameObject.collider, true);
		}else{
			Physics.IgnoreCollision(transform.collider, other.gameObject.collider, false);
		}
	}
	
	void OnCollisionExit (Collision other){
		//triggers before the box leaves the platform for some reason
		Physics.IgnoreCollision(transform.collider, other.gameObject.collider, false);
	}

The problem is as you can see in the video (0:35+) that the box won’t go through the object when i use OnCollisionEnter because OnCollisionExit triggers and i end up with a box that flashes like crazy :confused:

Got any clue how i can fix this? :slight_smile:

I’m not sure what the effect that you want, but from what I’ve seen, you should enable/disable the colliders on the objects themselves. The collider should be disabled until you exit the trigger.

Again, I’m not sure of the effect that you want. You’re only showning the bugs. Please explain the effect that you want.

I want the effect shown in the video from 0:00 to 0:35 but only affecting the box.

So for example, if i put another object on top of the red box and then drag the green box through the red box i want the object on top to stay unaffected but the green box should move through and change color.

To me its still very unclear what you want to achieve.

But maybe these pointer will help you.
If you need objects colliding with eachother, use OnCollisionXXXX()
If you want to ignore collision between objects, change their (collision)layer at runtime. See Unity - Manual: Layers for more info about that.

Okay, i will try to explain better :slight_smile:

If the button is in off mode the cube should not be able to go through the Platform but if the button is on the cube should go through the platform but the ball should stay on like nothing happened. The cube should be able to interact with the ball no matter what state the button is in.

(the button is just there to make it easy to explain)

I came to the conclusion that the problem is that the OnCollisionExit function triggers before the cube exits the platform for some reason, got any ideas why?

**EDIT:**I think that the OnCollisionExit function triggers when i do Physics.IgnoreCollision(transform.collider, other.gameObject.collider, true); because the other object is no longer touching the platform. One solution that i can come up with is to add an invisible trigger behind the platform and use OnTriggerExit instead. Is this the best way to go ?