Hey i want to Stop Raycasting when it hits a GuiTexture (if you touch / hit a GUI Element do not raycast or stop casting). Already tryed some stuff with Tags and Layers without any luck, hope someone can help me out.
GuiTextures are created with the Editor, not code (just moved and modified by code).
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
Collider colliders = Physics.OverlapSphere(hit.point, explosionRadius);
foreach(Collider c in colliders)
if (c.rigidbody == null) continue;
c.rigidbody.AddExplosionForce(explosionForce, hit.point, explosionRadius, upMod, ForceMode.Impulse);
Answer by robertbu
May 12, 2014 at 03:38 PM
You cannot use a Raycast() with a GUITexture. What you can do is use GUITexture.HitTest() to test if you've hit a specific texture. Or, you could create world colliders that tracks each GUITexture. To make the collider work, you'll need to convert the viewport position of the GUITexture to a world position. That can be done using Camera.ViewportToWorldPoint().
Thanks alot. Used this before Raycasting. if (gui1.HitTest(Input.mousePosition) || gui2.HitTest(Input.mousePosition)) return;
Answer by GDFenna
Mar 16 at 08:24 PM
You could use the z position and get the greatest gui to do touch events on.
AllGuiTextures = FindObjectsOfType<AddGuiButton>();
if(AllGuiTextures.Length != 0)
//Get closest z high
fZhight = -2;
iSaveHighZ = -2;
for(int l = MyFuncs.Zero; l < AllGuiTextures.Length; l++)
if(Input.touchCount != MyFuncs.Zero)
if(fZhight < AllGuiTextures[l].tMyTran.position.z)
fZhight = AllGuiTextures[l].tMyTran.position.z;
iSaveHighZ = l;
//Set the trigger for button touch
RootLink.InputControlMgrLink.isTouchingScreenBtn = !RootLink.InputControlMgrLink.isTouchingScreenBtn;
}//Amount of Touches
}//loop end textures
//do or action / press or hold
if(AllGuiTextures[iSaveHighZ] && Input.touchCount != MyFuncs.Zero)
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 navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
22 People are following this question.
Raycast function doesn't work.
Raycast between 2 points, how to get direction and length?
Way to lower raycast precision ? (to improve performance)
Using CrossHair GUI to Determine Ray Spread
Is this way of making a raycast correct