I’m rendering labels below game objects in an OnGUI function. I’m looking to only render labels when their associated game object isn’t obscured by other objects. To do this I want to cast a ray from the camera to the object. If the first object hit is the game object in question I know to render the label (as nothing else sits between it and the camera).
I've Googled and searched the Unity Answers website extensively. The article you linked to doesn't respond to my question (and instead refers to casting multiple rays within a scene)
The link provided in the comment actually does answer your question. It is the last answer so you have to scroll down.
Your solution is Linecast. It cast a line (thank you Cpt Obvious) from one position to another. If your line does not reach the other one then you have a collider in between.
So you need to use layer mask to ignore the collider of your player and the other guy.
EDIT: Based on coment, you may want to go for this one:
The documentation around Linecast seems like exactly what I'm after. But what I'm struggling with is how to cast a 'fat' ray/line from the camera into the scene - because casting a single line from one position in the camera viewport will surely not account for everything. Or do I have it backwards? Should I cast a line from each game object towards the camera?
please search here first on many many very very simir questions, for great answers and long explanations. 100s of such questions eh http://answers.unity3d.com/questions/445871/raycasting-crowds.html
– FattieI've Googled and searched the Unity Answers website extensively. The article you linked to doesn't respond to my question (and instead refers to casting multiple rays within a scene)
– Nialplease check out unity's physics layers system http://docs.unity3d.com/Documentation/Components/LayerBasedCollision.html if you search on here for "raycast" 100s of detailed responses
– Fattie