• 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 nhlarry · Jan 23, 2013 at 04:19 PM · guitextureraycastguitexturescreenpointtoray

Get GUITexture to follow ScreenPointToRay ray cast

I have a GUItexture that I would like to follow a raycast.

Current Setup:

         Vector3 Finger = GetWorldPos(fingerPos);
         float Finger_x = Finger.x / Screen.width;
         float Finger_y = Finger.y / Screen.height;
         float Finger_z = Finger.z;
         
         GameObject.Find("TestingFinger").transform.position = new Vector3(Finger_x,Finger_y,0);

         /* GetWorldPos function is as follows:
         public static Vector3 GetWorldPos( Vector2 screenPos )
         {
            Ray ray = Camera.main.ScreenPointToRay( screenPos );

            // we solve for intersection with z = 0 plane
            float t = -ray.origin.z / ray.direction.z;
            return ray.GetPoint( t );
          }
         */



However I am getting odd results. (0,0) is the bottom left of the screen and (1,1) is the top right of the screen.

When my mouse/finger is at (0,0) the Guitexture is at (1,1) I t$$anonymous$$nk the issue could be that the raycast is returning (0,0) as the middle of the screen. Any ideas how to convert t$$anonymous$$s so the GUItexture will follow the mouse. I have a video of how it is acting "odd" here: http://db.tt/c2H55tO8

Thanks.

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 FatWednesday · Jan 26, 2013 at 04:29 PM 0
Share

The difference i coordinate space between screen and gui has caused problems for me before too. but usually the process was only ever for debugging or temp stuff so never really looked for a clean workaround. One quick and probably dirty sollution is just to invert the coords your using at one end of the process.

so just 1-value it at see where that gets you.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Owen-Reynolds · Jan 26, 2013 at 06:11 PM

Won't the final position of the GUITex be just under your finger? Going from finger to world, then back to screen -- won't that just take you back where your finger was? If not, where? Could just do that with: Finger_X=Input.mousePosition.x/Screen.width; and get rid of the ray.

Say you wanted the GUIText to be centered over the object your finger is touc$$anonymous$$ng, then you'd need it all -- ray cast, take pos = HIT.transform.position, and back to the screen with Camera.main.worldToViewPort(pos).

But, in what you have, GetWorldPos is returning ray.GetPoint(), w$$anonymous$$ch is a spot in the game world, in meters. Then it gets used as pixels. In general that will give random results (based on camera pos.)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

GUI texture touch input problem 1 Answer

How can I add a distance cap to this script? 3 Answers

GUI.DrawTexture on GUI.Button press 1 Answer

GUI_TEXTURE PROBLEM! 2 Answers

GUI.Box not showing Texture on device 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