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 /
avatar image
0
Question by ALinnear · Aug 21, 2018 at 03:44 PM · camerauitransform.positiontrackingworldtoscreenpoint

How can I accurately convert a Game Object's position to a sub Canvas/Rect screen position?

Basically I am trying to position a black square (UI Element on the Canvas) at the same position as an object that the camera has focused on. The camera is attached to an aircraft which moves above the world space land that has various game objects. I have a RaycastHit that goes from the center of the camera's frustum out to the world where it registers all game object colliders and returns the GameObject VisualTarget for whichever hit.collider.gameobject it detects, this becomes the Target. In an attempt to have the black square track the targets position, I tried the following code:

Failed attempts at tracking the object accurately

 FocusTrack.transform.localPosition = RectTransformUtility.WorldToScreenPoint(mts.activeCamera, mts.VisualTarget.transform.position);
 
  FocusTrack.transform.localPosition=(mts.activeCamera.WorldToScreenPoint(mts.VisualTarget.transform.position));

The raycast used to get the collider positions

 Ray ray = activeCamera.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
        
         RaycastHit hit;
         if (Physics.Raycast(ray, out hit, 100000f, ~(1 << 9 | 12)))
         {
             Debug.DrawLine(activeCamera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0)), hit.point,Color.blue);
             if (!hit.transform.name.Contains("Node"))
             {
                 print("I'm looking at " + hit.transform.name);
                 VisualTarget = hit.transform.gameObject;
                 if (hit.collider.gameObject.GetComponent<MeshRenderer>() != null)
                 {
                     //hit.collider.gameObject.GetComponent<MeshRenderer>().material.color = Color.green;
                     //GetComponentInParent<Aircraft>().appManager.uiHud.SOC.GetComponent<SOC>().Track.transform.position = activeCamera.ViewportToWorldPoint(hit.point);
                     
                 }
                 else if (hit.collider.gameObject.GetComponentInChildren<MeshRenderer>() != null) {
 
                     //hit.collider.gameObject.GetComponentInChildren<MeshRenderer>().material.color = Color.green;
                 }
 
             }
             else
                 print("No object in viewpoint or no colliders detected.");
 
         }

So far it only somewhat tracks where the object is on the screen, but it is not very accurate at all and also tends to change its position (black box UI) depending on my current zoom level of the camera. A good working example would be:

  1. User locates the target.

  2. User grows a track on the target (black box) at it's center; the target is static, the user is moving

  3. If the user pans to the left, the black box, along with the target will appear more to the right

  4. If the user grows a track on the target and pans out of the target's view, the user shouldn't see the black box

  5. The black box stays at it's target's center at all times

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

0 Replies

· Add your reply
  • Sort: 

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

191 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 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

unwanted movements when camera follows object 0 Answers

How to get the actual World Positions of the Corners of an UI element when using GetWorldCorners if Canvas is set to Screen Space - World 2 Answers

Masking Camera Image / Removing Blur Offset 1 Answer

How can I continue redrawing my UI while generating a procedural world? 0 Answers

How to create a 2d interactive sprite planet? 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