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 BritishGuy · Jan 15, 2014 at 11:12 AM · 2dguiraycast3dfollow

GUI Follow RaycastHit

Hello,

Is it possible for a 2D GUI "Reticle" to follow a 3D RaycastHit point.

Kind of the same way World Of Tanks achieves there "Where turret is looking a GUI will follow"

Thanks.

EDIT: At the moment i am using a raycast from the gun, A plane that follows the raycast hit, and a billboard script on the plane with my reticle image on it, All works except it dose not keep its size the further away the raycast hits, Thats why i assumed a GUI would have been better as its glued to the screen? Any help would be greatly appreciated, Thanks.

EDIT2: i Attached a GUI Texture to a empty gameobject then made that Empty gameobject follow raycast hit Hoping the GUI Texture would follow on screen, It did but was extremely limited and sensitive and would not show on screen half the time, Maybe i am going the wrong way about doing this? 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 BritishGuy · Jan 15, 2014 at 01:43 PM 0
Share

Any Ideas? I have hit a brick wall...

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by robertbu · Jan 15, 2014 at 02:24 PM

Unity has five primary coordinate systems:

  • GUI: Starts in the upper left of the screen and uses screen pixels.

  • Screen: Starts in the lower left of the screen and uses screen pixels.

  • Viewport: Starts at (0,0) in the lower left of the screen and goes to (1,1) in the upper right.

  • World: 3D world where your standard game objects live. Positions are relative to the world origin.

  • Local: world coordinates relative to the position and rotation of an object rather than the world origin.

Your RaycastHit is in world coordinates. A GUITexture is in Viewport coordinates. A GUI.DrawTexture() is in GUI coordinates. A GUITexture is a good fit for what you are attempting to do. All you need to do is convert the coordinate from World to Viewport. Something like:

 transform.position = Camera.main.WorldToViewportPoint(hit.point);
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 stevethorne · Jan 15, 2014 at 02:31 PM 0
Share

This is great information to have, thanks!

avatar image
1

Answer by nesis · Jan 15, 2014 at 02:37 PM

Raycasting is the right way to go. Get hit.point and then use 'Camera.main.WorldToViewportPoint(hit.point)' to get what position your GUITexture should have. You might also want to perform a check that ensures the target the GUITexture is hovering over is still on screen (I do this below with a dot product measuring the direction the gun is facing and the direction from the gun to the reticle). Without this check, if you end up casting a ray backwards from the camera and hit something, Camera.main.WorldToViewportPoint() will possibly give you its position on the viewport (inverted since it's behind).

For example (in C#-ish code):

 public Transform gun;

 public void Update() {
     Ray ray = new Ray(gun.position,gun.forward);//or whatever you want your ray to be
     RaycastHit hit;
     if (Physics.Raycast(ray,out hit,Mathf.Infinity)) {
         Vector3 directionFromGunToHitPoint = (hit.point-gun.position).normalized;

         //the dot product of two normalized vectors measures 
         //the smallest angle between them... for our purposes,
         //if the dot product is greater than 0, we'll change
         //the reticle's position and draw it; else, we'll not draw it.
         float dotProduct = Vector3.Dot(directionFromGunToHitPoint,gun.forward);
         if (dotProduct>0) { //we're looking in the same-ish direction as where the raycast hit, yay
             Vector3 viewportPoint = Camera.main.WorldToViewportPoint(hit.point);
             reticle.position = viewportPoint;
             reticle.guiTexture.enabled = true;
         }
         else { //disable the guiTexture
             reticle.guiTexture.enabled = false;
         }
     }
 }
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 robertbu · Jan 15, 2014 at 02:46 PM 0
Share

@nesis - As an alternate to the dot product, just check the Viewport coordinate after converting. If the position is on the screen the viewport coordinate will be in the range of (0,0) to (1,1).

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 exclude OnGUI button from RayCastHit? 2 Answers

Fix a GUI's position relative to a 3D object 1 Answer

Raycast for 2d with touch. 0 Answers

2D Arrow pointing at 3D Position 1 Answer

A 2D GUI in mid air, for oculus, tracking an object. 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