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?
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.
Thank you both! I worked it out.
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
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users post good questions and use the site effectively we have posted a user guide.
If you are a new user, check out our FAQ for more information.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
For troubleshooting common problems with Unity 5.x Editor (including Win 10).
Answers and Comments
No one has followed this question yet.
Raycast Collider Tag not returning correct result
Is this way of making a raycast correct
Problem With Raycasthit Angle
Raycast returning incorrect collider when cast from screen point
Getting a point on ray