x


How can i disable raycast collision on a Pickup with isTrigger enabled ?

Hi All

I have a Pickup script attached to a simple object, this scripts make this object to rotate and oscillate, and ofcourse, if you hit it with your player, it disapears and become invisible thanks to 'renderer.enable = false'.

I need to say that this object ( a simple sphere ) have a sphere collider with 'isTrigger' option enabled, so when you hit it with player, OnTriggerEnter() is executed.

The main problem is when the renderer is disabled, collisions seems to stay up and active, and may camera's raycasts still found an spherical collider in this position. I repeat, sphere collider have 'isTrigger' enabled, but raycast still found a collider to collide with.

I tryed to disable the collider component with 'collider.active=false', but then, Pickup script is halt and no more calls to Pickup Update() function are made anymore, and ofcourse, i can't control how many time has been past to make the pickup visible again.

How can i disable this sphere collision wihtout shutting down Pickup's script ?

Thanks for your time :)

more ▼

asked Jan 12 '10 at 04:52 PM

Llorens Marti Garcia gravatar image

Llorens Marti Garcia
96 4 4 7

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

1 answer: sort voted first

just put it in a layer and use layermasks while doing raycasts. if you put the object in ignore raycast layer it will be done automatically for you in OnTriggerEnter event where you disable the rendering put a code like this

        LayerMask l = this.gameObject.layer;
    this.gameObject.layer = LayerMask.NameToLayer("Ignore Raycast");

this code will get the current layer and stores it in a variable and then change the layer to "ignore raycast" layer. you should define the variable l as a private variable in class scope in C# or outside your functions in js. then when you want to make the pickup visible and enable ray casting put a code like this.

        this.gameObject.layer = l;

take a look at reference for layers and LayerMask classes and raycast functions to get more info.

more ▼

answered Jan 12 '10 at 05:17 PM

Ashkan_gc gravatar image

Ashkan_gc
9.1k 33 56 117

Hi

Tested and works perfectly.

Also you need to raycast with 'Physics.Raycast(...)' and let it know which layer to ignore with 'LayerMask.NameToLayer("Ignore Raycast")' in the last parameter.

Thanks for your time :)

Jan 12 '10 at 05:32 PM Llorens Marti Garcia
(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:

x2485
x1525

asked: Jan 12 '10 at 04:52 PM

Seen: 2897 times

Last Updated: Jan 12 '10 at 04:52 PM