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
1
Question by bakkoto · Mar 23, 2015 at 05:38 PM · unity 5uiraycasteventsystem

IsPointOverGameObject() not working as intended!!!

Like many others I tried also to block/prevent Physics.Raycast from going through UI elements (canvas) using "EventSystem.current.IsPointerOverGameObject()" but unfortunately without success..

Im not sure why, but the "EventSystem.current.IsPointerOverGameObject()" is reacting to all UI gameobjects (what I´m looking for), but also to any gameobject on my scene that have an active collider attached to it. In other words it returns true for UI and non-UI gamebojects!!

For the Physics.Raycast part, Im using a simple function that Im calling inside Update() :

    protected void HandleInput()
    {
     Ray ray = RayCam.ScreenPointToRay(Input.mousePosition);
     RaycastHit hit;
     float _rayDistance = Mathf.Infinity;
     LayerMask _rayMask = ( 1 << this.UnitsLayer | 1 << this.GroundLayer);
     
     if (Physics.Raycast(ray, out hit, _rayDistance, _rayMask))
     {
       // *** Ray hit a Unit
       if (hit.collider.gameObject.layer == this.UnitsLayer){// do something}
       // *** Ray hit the Ground
       if (hit.collider.gameObject.layer == this.GroundLayer){// do something else}
     }
    }

Isn´t "IsPointerOverGameObject()" supposed to return true only if the EventSystem object is an UI element??

Notice : adding more conditions check like "EventSystem.current.currentSelectedGameObject.layer == 5" didn´t solve the problem neither because the "EventSystem.current.currentSelectedGameObject" is always returning NULL.

Caching the EventSystem (the one created inside the Canvas) into a variable "myEventSystem" and going through the same process didn´t change anything, too.

What am I doing wrong? Any suggestions to solve this problem?

Many thanks in advance.

Comment
Add comment · Show 1
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 bateleur · May 30, 2015 at 03:13 PM 0
Share

The reason EventSystem.current.currentSelectedGameObject isn't doing what you want is because it relates not to what the mouse is over, but to "the selection", which most other UIs call the keyboard focus.

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by LesterZw · May 19, 2015 at 09:01 AM

EventSystem.current.IsPointerOverGameObject() will return true for non-ui gameobjects if there is a PhysicsRayCaster attached to one of your gameobjects.

Either remove the PhysicsRayCaster or modify PhysicsRayCaster.EventMask, in my case i had to switch off TransparentFX.

Comment
Add comment · Show 2 · 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 luislodosm · Jan 14, 2018 at 09:47 PM 0
Share

I fixed this with PhysicsRayCaster > Event$$anonymous$$ask > Nothing

avatar image Honorable-Chow · Dec 16, 2018 at 04:55 PM 0
Share

In my case I was using a camera component that had come with a PhysicsRayCaster on it, once I removed it then it stopped being true all the time.

avatar image
0

Answer by Mykhaylo-Hnatyuk · Dec 14, 2015 at 02:01 PM

In my case IsPointerOverGameObject returns TRUE if(touch.phase == TouchPhase.Began), BUT it returns FALSE if(touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)

So you can remember if you started your touch over UI and cancel it at the end.

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

26 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

Related Questions

Different between IPointerClickHandler.OnPointerClick and MonoBehaviour.OnMouseDown 1 Answer

XR UIHelpers Eventsystem ray is coming out of my main camera rig instead of my hands 0 Answers

Allow Click Through some UI Elements 1 Answer

Raycast from 2D canvas object to world space 1 Answer

Redirect UI Events to non visible UI Hierarchy 0 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