• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Confuse about transform.InverseTransformPoint 1 Answer

UI is invisible 1 Answer

Why does my TMP inputfield text disappears when I rotate It, why using a world space camera 0 Answers

UI elements within canvas are invisible 4 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges