Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Totalschaden · May 12, 2014 at 03:35 PM · guiraycastraycastingstoppassthrough

Stop Raycast from passing though GUI

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

Raycast Code:

         if (Input.GetKeyUp(KeyCode.Mouse0)) 
         {
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast(ray, out hit))
             {
                 Debug.Log(hit.point);
                 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);
                 }
             }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Totalschaden · May 12, 2014 at 04:11 PM 0
Share

Thanks alot. Used this before Raycasting. if (gui1.HitTest(Input.mousePosition) || gui2.HitTest(Input.mousePosition)) return;

Works perfect

avatar image
0

Answer by GDFenna · Mar 16, 2017 at 08:24 PM

You could use the z position and get the greatest gui to do touch events on.

 float fZhight;
         int iSaveHighZ;
     AddGuiButton[] AllGuiTextures;
     void DoTouchOnGuiTextures()
     {
         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(AllGuiTextures[l])
                 if(Input.touchCount != MyFuncs.Zero)
                 {
                     if(AllGuiTextures[l].thisGuiTexture.HitTest((Vector3)Input.touches[0].position))
                     {
                         if(fZhight < AllGuiTextures[l].tMyTran.position.z)
                         {
                             fZhight = AllGuiTextures[l].tMyTran.position.z;
                             iSaveHighZ = l;
                         }
                         //Set the trigger for button touch
                         if(!RootLink.InputControlMgrLink.isTouchingScreenBtn)
                             RootLink.InputControlMgrLink.isTouchingScreenBtn = !RootLink.InputControlMgrLink.isTouchingScreenBtn;
                     }//Hit True
                 }
                 else
                 {
                     //Reset
                     AllGuiTextures[l].SetupTexture(0);
                 }//Amount of Touches
             }//loop end textures
 
             //do or action / press or hold
             if(AllGuiTextures[iSaveHighZ] && Input.touchCount != MyFuncs.Zero)
                 AllGuiTextures[iSaveHighZ].touchUnity(0);
         }
     }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

DrawRay not drawing a ray 1 Answer

Help Understanding Raycast 2 Answers

Raycasting ignoring small sized object 1 Answer

raycastHit.point - How do I make it ignore colliders? 1 Answer

Raycasting not working on a procedural mesh with a mesh collider. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges