- Home /
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
One thought I had was to add all hits to an array and only activating the top most item...
Answer by Bunny83 · Mar 24, 2011 at 04:42 PM
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.
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.
Answer by AVividLight · Mar 24, 2011 at 03:44 PM
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