x


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; }

more ▼

asked Nov 12 '10 at 03:14 AM

Avery gravatar image

Avery
13 5 5 11

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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.
more ▼

answered Nov 12 '10 at 04:39 AM

Tom 10 gravatar image

Tom 10
287 2 3 7

thanks for the suggestion Tom, however i have no idea how to use the Physics.IgnoreLayerCollision. >< is there any examples out there i could follow? thanks!

Nov 15 '10 at 01:20 AM Avery

How you use Physics.IgnoreLayerCollision depends on how you're creating the carrot and player GameObjects. If you have a prefab for each color of carrot and for each color of player, then you can accomplish this in the Editor without writing any code. I'll update my answer to include instructions for this case.

Nov 15 '10 at 04:42 PM Tom 10

ty works perfectly!!!

Dec 22 '10 at 09:46 AM amit
(comments are locked)
10|3000 characters needed characters left

You can use something like

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

Then turn detectCollisions true whenever you wish

more ▼

answered Nov 13 '10 at 05:15 PM

towerer gravatar image

towerer
108 3 4 10

thanks for the suggestion however when i tried that method, it doesnt seems to work, i updated my code above. ;)

Nov 15 '10 at 01:19 AM Avery

What kind of a problem does this method cause? As you probably know the script has to be placed in the rigidbody object, and do not forget to add "rigidbody.detectCollisions = true;" after the if else lines

Nov 16 '10 at 03:31 PM towerer
(comments are locked)
10|3000 characters needed characters left

ty!!!! works perfectly

more ▼

answered Dec 22 '10 at 09:45 AM

amit gravatar image

amit
16 5 5 5

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2482
x1864
x1781

asked: Nov 12 '10 at 03:14 AM

Seen: 2002 times

Last Updated: Nov 15 '10 at 01:19 AM