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 sevensixtytwo · Apr 07, 2015 at 07:06 AM · camerauiraycast

Raycast from GUI element

How would I raycast from a GUI element? For example, I have a crosshair that lazily follows the center of the screen or can be offset according to the needs of the player. I would like to keep raycasting from the crosshairs regardless of where it is at that moment.

I have tried

 camera.ViewportToWorldPoint(guiRect.anchoredPosition);

To no avail.

Note that the crosshair is parented to several other UI elements which also move.

Thanks for your time.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by ethestel · Apr 07, 2015 at 07:34 AM

Hello,

Try this:

 Ray ray = Camera.main.ScreenPointToRay(uiObject.transform.position);
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 sevensixtytwo · Apr 07, 2015 at 08:07 AM 0
Share

I used that and it worked well back when I had GUITextures but nothing happens with 4.6 GUI elements.

avatar image ethestel · Apr 07, 2015 at 08:22 AM 1
Share

To debug if ray is working or not, you can add this script to any game object

 using UnityEngine;
 using UnityEngine.UI;
 
 public class RayFrom : $$anonymous$$onoBehaviour {
 
     private Ray m_ray;
     public RectTransform r_canvasElementTransform;
 
     void Update()
     {
         m_ray = Camera.main.ScreenPointToRay(r_canvasElementTransform.position);
     }
 
 
     void OnDrawGizmos() {
         Gizmos.color = Color.yellow;
         Gizmos.DrawRay(m_ray.origin, m_ray.direction*1000f);
     }
 
 }
 

and reference the GUI element you want to shoot ray from. When you go into play mode, you should see a yellow line go out from your camera object on editor view. Try moving the canvas element around to test if yellow line is moving

avatar image sevensixtytwo · Apr 07, 2015 at 10:01 AM 0
Share

It seems to be shooting towards the bottom left corner of the screen or towards the sky and rightwards.

This happens to both viewport.ViewportPointToRay(crosshairRect.position) and viewport.ViewportPointToRay(crosshairRect.anchoredPosition)

All I really need is a way to get a UI element's canvas position and convert it to viewport position so I can cast a ray from it.

avatar image ethestel · Apr 07, 2015 at 10:47 AM 0
Share

Is your result similar to this? alt text

screen-shot-2015-04-07-at-112053.png (66.0 kB)
avatar image sevensixtytwo · Apr 09, 2015 at 04:52 AM 0
Share

Close enough. In $$anonymous$$e, the ray comes from the camera and is aligned with the lower left corner frustum with

 viewport.ViewportPointToRay(crosshairRect.anchoredPosition)

while

 viewport.ViewportPointToRay(crosshairRect.position)

makes the ray go from the camera to the center of the canvas in world space.

I just noticed I had put ViewportPointToWorld in the OP. That's a mistake. xD

Show more comments

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

19 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

Related Questions

How to use both NGUI widgets and unity UI 0 Answers

Is it possible to configure raycast to hit opaque pixels without using alphaHitTestMinimumThreshold? 1 Answer

Block camera raycast by UI 1 Answer

Unity Xr Mesh Raycast,Unity XR Mesh Raycast 0 Answers

Allow Click Through some UI Elements 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