I have a project that lets me select different walls and colorize them upon clicking. However, when I change the sliders in the GUI, the clicks are registered both in the GUI and in the selection code for the walls. How can I change this so that clicks in the GUI aren't registered elsewhere?
Specifically, in select.cs, I've tested with a separate layermask for the walls:
void Update() {
//check if the left mouse has been pressed down this frame
if (Input.GetMouseButtonDown(0))
{
// empty RaycastHit object which raycast puts the hit details into
RaycastHit hit;
// ray shooting out of the camera from where the mouse is
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
LayerMask mask = (1 << LayerMask.NameToLayer("Walls"));
if (Physics.Raycast(ray, out hit, Mathf.Infinity, mask.value) &&
hit.transform.name == this.name)
{
//print out the name if the raycast hits something
if(!selected) {
this.renderer.material = mat;
selected = true;
} else {
this.renderer.material = origMat;
selected = false;
}
}
}
}
Instead of polling the mouse in Update(), you could also check for MouseDown events in your OnGUI() function, and have that invoke the raycast. If you have all your other GUI code before that, you will only get MouseDown events which have not been used by any other GUI elements before (as the builtin GUI elements will all change the event type to "Used" when you interact with them).
there is another method too. use a GUITexture on top of the gui and test if mouse is on it or not. if the mouse is not in gui's rectangle then raycast for walls.
this method has an advantage over other methods, you can ignore clicks in a rectangular area. so clicks inside your guis will not be registered for walls.
testing against guiTextures is easy, use their HitTest method.
I was wondering if some one could help me? i am making a game in unity and i cant get something to work, basically what i want to do is if my play walks next to an object i want it to show my 3d text. i have made the 3d text invisible when the game starts i have placed a cube which has a raycast on it. when i go next to the box the 3d text should appear up can any one help me plz... here is the script that i have come up with
function update (){
gameObject.active = false;
}
function update (){ var hit : RaycastHit;
if (Physics.Raycast (transform.position, transform.forward, hit,6 )){ if (hit.collider.gameObject.tag == "Box and text " ){ (gameObject.tag == "Press X"); gameObject.active = true ;
} }}
this first line basically makes the 3d text on the box disappear as soon as the game is loaded then the next part is where the Raycast comes in. its basically saying if i am 6 mitres next to the cube show the 3d text please can you help. also the scrip is attached to the 3d text i have also tried to connect the script to the cube but same thing happens if i am in the right distance the 3d text does not appear