x


RayCast hit: one object at a time

Hello,

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

Haigotron gravatar image

Haigotron
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

Bunny83 gravatar image

Bunny83
78.8k 27 110 344

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

AVividLight gravatar image

AVividLight
2.1k 209 142 196

(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:

x2878
x342
x292

asked: Mar 24, 2011 at 03:29 PM

Seen: 9751 times

Last Updated: Jul 13 at 09:55 AM