Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by JNylander · May 25, 2017 at 12:38 PM · uicanvasraycastingeventsystemraycastall

EventSystem raycasting on World Canvas gameobject always returning worldPosition of zero

I'm trying to get the worldposition of a raycast hit from the EventSystem module. The problem is that the returned worldposition value of the RaycastResult is always Vector3.zero. The distance and gameobject values are correct.

PointerEventData pointerData = new PointerEventData (EventSystem.current);

     pointerData.position = Input.mousePosition;

     List<RaycastResult> results = new List<RaycastResult>();

     EventSystem.current.RaycastAll(pointerData, results);

     print (results.Count);

     for (int i = 0; i < results.Count; i++) {
         print (results [i].worldPosition);
     }
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
2

Answer by JNylander · May 26, 2017 at 10:35 AM

We'll I dug through the Unity UI Bitbucket repository and found out that GraphicRaycaster.Raycast method doesn't even set a value for RaycastResult.worldPosition, only screenPosition. This is probably why EventSystem.current.RaycastAll doesn't return a worldPosition. The repository points towards Unity version 5.2 but I doubt anything has changed in this regard.

My inelegant solution was to use the screenposition from RaycastResult with Camera.main.ScreenpointToWorldPoint, with RaycastResult.distance as the Z-axis of screenposition. I'll edit this post to tell if this works or not.

[GraphicRaycaster sourcecode from Bitbucket] https://bitbucket.org/Unity-Technologies/ui/src/0155c39e05ca5d7dcc97d9974256ef83bc122586/UnityEngine.UI/UI/Core/GraphicRaycaster.cs?at=5.2

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

Answer by UnityBrains · Apr 15, 2018 at 07:05 PM

TL;DR Use the physics system to get a world space result for UI raycasting.

I know this is an old thread, but the problem appears to still be present this year.

Thought I'd offer up my solution;

I utilized the PHYSICS raycasting system to get a mouse click world position. I made a game object with only a mesh collider object (a quad in my case, but any physics collider object would work), put the mesh collider on the UI layer and did a regular physics raycast against that.

 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 RaycastHit rHit;
 Physics.Raycast(ray, out rHit, Mathf.Infinity, LayerMask.GetMask("UI"));
 gameObject.transform.position = rHit.point;

From here, you can obviously do checks to ensure you're using the correct raycast hit results. I was able to skip that because this quad was the ONLY object in the UI layer that could return a physics raycast result.

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

148 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 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 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 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 I disable/enable my canvas (pause game menu), controller navigation in the canvas stops to work? 1 Answer

OnPointerEnter blocked by something 2 Answers

EventSystem.current is returning null 0 Answers

UI buttons don't work with Canvas Render Mode: Screen Space - Camera. 2 Answers

How to render an image always on top? Already tried some solutions 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