Raycasting to determine whether an object is visible from camera

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

What’s the best way to go about achieving this?

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

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)

please 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

1 Answer

1

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.

You can see here also: unitygems.com - unitygems Resources and Information.

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?

what is "myColor"? I am a little confuse. Is it the color of the platform? If so, can you show me the code you used to assign "myColor". Thank You