Allowing player to pass through a gameObject with rigidbody if condition are not fulfilled? HELP PLEASE!!!!

I was wonder if there is a way to allows a moving player to pass through a game Object with rigidbody attached to it. For instances, i want the player to collect the carrots only if both have the same matching colours. Else the player will just pass thru the carrots.

Is there a way to achieve this??

Please, any suggestion. Thanks alot.

function OnCollisionEnter( collision : Collision ) { if(collision.gameObject.tag == "Carrot1" && playerNo == 1 && carrotNo == 1) { Destroy(collision.gameObject); scores.addScore(); scores.addMultiply(); } else { rigidbody.detectCollisions = false; }

OPTION #1

Take a look at Physics.IgnoreLayerCollision.

If you have one carrot prefab whose color you're setting in script, then you can put all carrots of the same color in one layer (use the "layer" property of the GameObject), and enable/disable collisions with carrots of that color using Physics.IgnoreLayerCollision.

OPTION #2

But if you have 1 prefab for each color of carrot and 1 prefab for each color of player, then you can set up the collision rules you want in the Editor.

  1. In the upper right, click on "Layers" and then "Edit Layers..." Create 1 layer for each color.
  2. In the prefab for each carrot and player, set the layer for that prefab to be the correct color layer.
  3. On the menu bar, click Edit -> Project Settings -> Physics. Under "Layer Collision Matrix", you can uncheck the appropriate boxes to disable collisions between different colors.

You can use something like

OnCollisionEnter(){ if (colorMe!=colorCarrot){ rigidbody.detectCollisions = false; }

Then turn detectCollisions true whenever you wish

ty!!!! works perfectly