- Home /
Raycasting and Layer Masks problem
I want to have various 'info points' set up across my scene that contain a unique ID(stored as an int). When the player clicks on one of these points a GUI window will pop up and display the relevant info based on the supplied ID. I'm using raycasting and setting a layer mask to make contact with the info points. This works great with one info point...
However, when I add more than one I'm getting one window for each info point display at the same time. I can understand there may be a conflict here from using one layer mask - Is there any way to filter these objects based on their ID or will I need to take a different approach on this?
Answer by Fattie · Dec 15, 2011 at 06:56 AM
Unless I misunderstand your question, it's fortunately very easy -- just use the .name of the collider you have hit ..
inside Update() on something attached to the scene...
if ( Input.GetMouseButtonDown(0) )...
yellowRay = theCamera.ScreenPointToRay( Input.mousePosition ) ..
if ( Physics.Raycast(yellowRay, theHit, 5.0) ..
.. print("name is .. " + theHit.collider.name_)
.. if (theHit.collider.name == "myFirstSignThing")
.. if (theHit.collider.name == "myOtherSignThing")
and so on. If that doesn't do it for you, you probably just have some basic code error unrelated to the physics and you'd have to post the code. Hope it helps.
Thanks Fattie, I understand your logic and have used it to fix this problem. It is a fairly simple bit of code to add! I'm still having a little trouble with Window IDs but that's a question for another day. :) Thanks again.
Answer by Owen-Reynolds · Dec 17, 2011 at 08:30 PM
You can also use OnMouseDown(), or similar, and lose the raycast. Something like:
public int infoID;
public string someText;
void OnMouseDown() {
GameObject.Find("infoGUI").GetComponent<infoGuiScript>().show(infoID, someText);
}
infoGuiScript is attached to the one infoGUI object, which swaps out content on each call to show.
Your answer
Follow this Question
Related Questions
c# layermask in raycasting doesn't work !!! 2 Answers
Find closest point on all objects in a layermask 1 Answer
Help Can't find the Correct Parameters 1 Answer
Layer Mask Detection 2 Answers
Raycast layers 1 Answer