Before updating to Windows 10 you may need to return your Pro license. More information here

RayCast hit: one object at a time


Currently, I have a raycast originating from the player and going in the direction of where he's looking.

Using the raycast the player can activate objects that are in front of him. However, I want to limit the number of objects hit by the raycast to just 1. (This object is not necessarily the closest one.)

Is there a clean and efficient way to make sure only one object is only being hit by the raycast and not multiple objects at the same time?

Thanks again

more ▼

asked Mar 24, 2011 at 03:29 PM

avatar image

42 7 6 14

One thought I had was to add all hits to an array and only activating the top most item...

Mar 24, 2011 at 03:31 PM Haigotron
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

First of all the "normal" Raycast returns only one object. It returns the first one it hits. You don't specified your conditions what you're after. It sounds like you want to ignore some colliders? The easiest way to ignore some colliders from being raycasted would be to put them on the "Ignore Raycast" layer.

If you want to selectively choose object groups you can also specify against what layers you want to raycast. Just take a look at Layers.

If your object criteria is something else you need to use Physics.RaycastAll to get an array of all colliders that lie on your ray and sort them out. Can you tell us what's your criteria? If there are multiple objects and you want to get a specific result you have to know how to distinguish between those objects.

more ▼

answered Mar 24, 2011 at 04:42 PM

avatar image

97.1k 54 300 528

Thank you both! I worked it out.

Mar 29, 2011 at 06:47 PM Haigotron

The second sentence is very useful information. Thank you. Docs say "any" collider, although it wouldn't be very practical. It's kind of confusing.

Jul 13, 2014 at 09:55 AM Simon Says
(comments are locked)
10|3000 characters needed characters left

Hey Haig James,

You could only call the ray when the mouse is pushed... So whatever the ray is on when you click, it picks up...

-Hope I Helped; Gibson

more ▼

answered Mar 24, 2011 at 03:44 PM

avatar image

2.2k 210 143 198

(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



Answers and Comments



asked: Mar 24, 2011 at 03:29 PM

Seen: 11195 times

Last Updated: Jul 13, 2014 at 09:55 AM