Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
This question was closed Oct 09, 2015 at 09:54 PM by Ashky for the following reason:

The question is answered, right answer was accepted

avatar image
3
Question by Ashky · Oct 09, 2015 at 12:46 PM · uiraycasttouchlayermask

How can I prevent my raycast from passing through UI?

Hello Unity Community!

I have this piece of code here:

     private void CheckTap(){
         if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began){
             //if(EventSystem.current.IsPointerOverGameObject()){    
                 RaycastHit h;
                 Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
                 if(Physics.Raycast(ray,out h,15.0f,lmask)){
                     if(h.transform.gameObject.tag=="Square") {
                         h.transform.gameObject.GetComponent<script_SquareHub>().OnHit();
                     }
                     else if(h.transform.gameObject.tag=="Bumper"){
                         bumperHit.Play();
                         switch(livesRemaining){
                         case 3: l3.GetComponent<Image>().color=new Color32(205,191,172,150);
                             livesRemaining=2;
                             break;
                         case 2: l2.GetComponent<Image>().color=new Color32(205,191,172,150);
                             livesRemaining=1;
                             break;
                         case 1: l1.GetComponent<Image>().color=new Color32(205,191,172,150);
                             livesRemaining=0;
                             break;
                         case 0: _timeRemaining=0.0f;
                             break;
                         }
                     }
                 }
             //}
         }
     }

It works fine on detecting various objects using touch. However, the ray goes through UI, so when I try to pause my game the ray will hit my "Bumper" and cause the player to lose a life.

How can I prevent my ray from passing through the UI? As you can see I've tried using IsPointerOverGameObject, but to no success, and setting the layer mask lmask to include UI doesn't yield the results that I want. Any solution to this?

Comment
Add comment · Show 2
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 meat5000 ♦ · Oct 09, 2015 at 12:49 PM 0
Share

doesn't yield the results that I want

explain?

Layermask should be the perfect solution.

Could you detail here, how you set up your layermask?

avatar image Ashky meat5000 ♦ · Oct 09, 2015 at 02:15 PM 0
Share

Well, my layermask (`lmask` in the code) is a public layermask, that I set in the editor to contain only the UI, Square, and Bumper layers. Here's how it looks in the editor: alt text

Canvas has UI set as its layer by default, and I manually set up the Bumper and Squares layers. $$anonymous$$y ray interacts with those, but ignores UI.

mwe2kvn.png (22.9 kB)

3 Replies

  • Sort: 
avatar image
14
Best Answer

Answer by fafase · Oct 09, 2015 at 07:58 PM

You had it going well but were missing one extra info.

 private int fingerID = -1;
 private void Awake()
 {
 #if !UNITY_EDITOR
     fingerID = 0; 
 #endif
 }
 private void Update()
 {
     if (EventSystem.current.IsPointerOverGameObject(fingerID))    // is the touch on the GUI
     {
        // GUI Action
        return;
     }
     // Your raycast code
 }

The problem is in editor the parameter for the IsPointerOverGameObject needs to be -1.

Comment
Add comment · Show 6 · 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 Ashky · Oct 09, 2015 at 08:55 PM 0
Share

Thanks for your answer. But what does EventSystem.current.IsPointerOverGameObject() actually show me? I'm just interested in running my raycast code when I'm not over UI, since I handle the UI functionality from somewhere else. Also, it only needs to only work in the build, I'm not going to be using the editor anymore since this is the final issue that I have to solve before publishing my project.

avatar image fafase · Oct 09, 2015 at 09:32 PM 0
Share

EventSystem is the class taking care of all the interfaces concerning the UI (pointer dragged, enter, exit and so on). The method returns true if your pointer is over the UI. As a result, the raycast is not happening since there is a return in the if-statement. If the pointer is not over a UI element, then it is false and the raycast is performed. Since you are dealing with UI elsewhere, nothing else but the return happens.

You can also turn it the other way:

  if (EventSystem.current.IsPointerOverGameObject(fingerID) == false){
     // Raycast
 } 

The parameter is different in Editor and mobile (dunno about standalone). So for the same system to work on mobile, it requires the 0 parameter.

avatar image Ashky fafase · Oct 09, 2015 at 09:51 PM 0
Share

Yes, this works perfectly! Thank you! You, Bored$$anonymous$$ormon and AngryBurritoCoder helped me a lot with my first project, and now it's complete. You guys rock!

avatar image Ashky fafase · Oct 09, 2015 at 10:07 PM 0
Share

@fafase, as a side note: am I the only one that thinks this method is horribly named compared to what it does? You'd think IsPointerOver*GameObject*(fingerID) == true means that the pointer IS over an object so you should raycast without worry, when ins$$anonymous$$d, it's just the opposite. At least that's what the name suggests.

avatar image fafase · Oct 10, 2015 at 07:05 AM 0
Share

I guess it is meant to be over a relevant game object, a UI game object in that case. But yes, it could be confusing. When you build something as big as Unity, comes the times when you run out of appropriate na$$anonymous$$g.

avatar image SSStreetFighter · Jan 07, 2021 at 06:00 AM 0
Share

This didn't work for me. $$anonymous$$y agent only moves toward UI elements and can't see touches on the map. What am I doing wrong?

public class $$anonymous$$ovement : $$anonymous$$onoBehaviour { public GameObject RallyPoint; private int fingerId = -1;

 private void Awake()
 {

if !UNITY_EDITOR

 fingerID = 0; 

endif

}

 void Update()
 {
     if (Input.touchCount > 0)
     {
         RaycastHit hit;
         Touch touch = Input.GetTouch(0);
         Vector3 touchPosition = touch.position;
         Ray ray = Camera.main.ScreenPointToRay(touchPosition);
         if (Physics.Raycast(ray, out hit, $$anonymous$$athf.Infinity))
         {
             if (EventSystem.current.IsPointerOverGameObject(fingerId))
             {
                 Vector3 newPosition = new Vector3(hit.point.x, hit.point.y, hit.point.z);
                 RallyPoint.transform.position = newPosition;
             }
         }
     }

 }
 

}

avatar image
2

Answer by meat5000 · Oct 09, 2015 at 02:19 PM

Physics raycast doesnt work on new UI I do believe.

You need a Graphic Raycaster

http://docs.unity3d.com/Manual/script-GraphicRaycaster.html

Comment
Add comment · Show 9 · 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 Ashky · Oct 09, 2015 at 02:47 PM 0
Share

How could I use it? The Canvas has a graphics raycaster by default. How should I modify my current code to accommodate this change?

avatar image meat5000 ♦ Ashky · Oct 09, 2015 at 02:58 PM 0
Share

Looking at the Scripting API, it appears you need to pass it some Pointer Event Data.

http://docs.unity3d.com/ScriptReference/UI.GraphicRaycaster.Raycast.html

Perform a base raycast but ins$$anonymous$$d of Physics.Raycast, use GraphicRaycaster.Raycast

avatar image Ashky meat5000 ♦ · Oct 09, 2015 at 02:59 PM 0
Share

But if I use this method won't the graphic raycaster ignore my objects?

Show more comments
avatar image GrossPaul · Aug 25, 2021 at 02:39 PM 0
Share

Works for me with this adaption: public static bool IsPointerOverGameObject() { //check mouse if (Input.touchCount == 0 && EventSystem.current.IsPointerOverGameObject()) { return true;

         }
 
         // Check touches
         for (int i = 0; i < Input.touchCount; i++)
         {
             var touch = Input.GetTouch(i);
             if (touch.phase == TouchPhase.Began)
             {
                 if (EventSystem.current.IsPointerOverGameObject(touch.fingerId))
                 {
                     Debug.Log(touch.position);
                     return true;
                 }
             }
         }
 
         return false;
     }
avatar image
1

Answer by Kiwasi · Oct 09, 2015 at 09:49 PM

This may also help

http://m.youtube.com/watch?v=EVZiv7DLU6E

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 Ashky · Oct 09, 2015 at 09:54 PM 0
Share

Yeah, I saw your video before I posted this question. I was using method 1, the quick and dirty one, but I was using it incorrectly. But thank you for your help. You, fafase and AngryBurritoCoder all helped me a lot with my first project!

Follow this Question

Answers Answers and Comments

44 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 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 avatar image avatar image

Related Questions

if ( Physics.Raycast ( ray, out hit, Mathf.Infinity, LayerMask ) ) not working? 2 Answers

How to get PointerEventDatas[]? 0 Answers

raycasting and layermask question 2 Answers

Holding down UI Button and shooting raycast from touch position doesn't work simultaneously:(( 0 Answers

How to touch select 3D objects 2 Answers


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