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 EsoEs · Nov 11, 2014 at 06:58 PM · ray4.6

Raycasting in new GUI

So I'm a little confused exactly how to implement Rays in the new GUI.

1) If I'm using a ScreenSpace Overlay canvas, what sort of Physics am I using to detect hits on different objects on the panel, 2D or 3D? Could you provide an example of a code used to detect such a hit?

2) Do I need to add another camera or can I attach the script to the main camera?

3) Do I need a 2D or 3D box collider on my gameobjects?

+anything other information that may be relevant, thanks!

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
6
Wiki

Answer by CanisLupus · Nov 22, 2014 at 03:29 PM

Generally speaking, to raycast against uGUI objects you can do like I did here. Just notice that my example requires camera.WorldToScreenPoint, because my Canvas is in World Space and I had a world position, but, to raycast, we require a screen position. We'll get there in a moment.

To answer your questions:

1) You shouldn't need the Physics class for uGUI objects, whatever Canvas mode you're using. An object can be hit by EventSystem.current.RaycastAll(pointerEventData, raycastResults) when it has a component that is a possible target of the configured raycasters (see the link I gave you for more code). For example, a Canvas has a GraphicRaycaster attached by default, which allows EventSystem.current.RaycastAll to hit uGUI elements that are Graphics (Image, Text, etc). PhysicsRaycaster is another raycaster that allows raycasts against colliders (there are two: PhysicsRaycaster and Physics2DRaycaster) and requires a Camera.

2) You can put the script that raycasts anywhere. uGUI Raycasts use 2D (screen) positions. If you already know the 2D position to cast the ray from, ignore what I'm about to say.

If you have a 3D (world) position and your canvas is in a mode that uses a camera, you'll likely need to convert the world position into a screen position (to then raycast from there). Again, the code in my link does that :) If - as is your case - your Canvas is in Overlay mode, world positions (discarding Z) already correspond to screen positions, so you shouldn't need the conversion. Unless I'm mistaken. If I am, try using the InverseTransformPoint method of RectTransform to convert them. Use it like canvasRectTransform.InverseTransformPoint(someWorldPosition).

3) Colliders are required for the Physics' Raycast methods (either 2D or 3D) and for the PhysicsRaycaster, when used as as one of the possible raycasters in the EventSystem. However, if you simply want to raycast against Graphic uGUI objects, you don't need colliders and can rely on the GraphicRaycaster. :)

Many thanks to Xtro for correcting me, as I didn't know about the PhysicsRaycaster and was spreading misinformation. I modified a great deal of this answer, so please correct me if needed or edit my answer (I changed it to community-editable).

Comment
Add comment · Show 3 · 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 Xtro · Feb 09, 2015 at 06:38 PM 0
Share

I downvoted because number 3 is wrong. Colliders are required for EventSystem and PhysicsRaycaster to work properly on non-ui gameobjects.

avatar image CanisLupus · Feb 09, 2015 at 11:44 PM 1
Share

@Xtro Thanks for warning me that I was wrong! :) I don't like spreading wrong information. :s I didn't know that those PhysicsRaycasters existed, so I was wrong on a few levels and greatly edited the answer.

Also, originally I said that any object with a RectTransform could be hit by RaycastAll of the EventSystem. Actually, GraphicRaycaster seems to work only on Graphic components (as the documentation also implies). I suppose that a custom raycaster could be created to target RectTransforms, and that it doesn't exist by default. Or does it?

If my answer still has factual errors, please fire away. :)

avatar image Wellmann · Aug 18, 2016 at 09:45 AM 0
Share

Be aware that Rect Transform of the object has to be bigger than the Collider. Otherwise the Collider will be trimmed by the Rect Transform bounds.

avatar image
0

Answer by Bitpocketer · Feb 06, 2016 at 12:36 PM

@cansilLupus, that's the best thing i have ever learned on community thank you very much be blessed

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 Bitpocketer · Feb 06, 2016 at 12:37 PM 0
Share

@canisLupus, pardon for typo, I was trying to interact with UI using oculus and that's exactly what got me into it.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

SORPG: Distance problem? 1 Answer

Confused as to how my RaycastHit script works 1 Answer

Shooting with touch 1 Answer

Ray direction, projection matrix 0 Answers

Editor mousePosition changing when key is pressed 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