Author Topic: Placing GUI elements based on object in scene  (Read 3071 times)

zetain

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 8
    • View Profile
Placing GUI elements based on object in scene
« on: July 08, 2014, 10:01:52 AM »
I don“t know if this was asked before but i could not find a thread for this one.

So here is my problem:
I have 2 cameras. One displays the scene and all objects the other one the gui.
I want to place a Sprite at the position of one of the objects.
I tried using
        uiCamera.WorldToScreenPoint(theObj.transform.position);
and
        uiCamera.WorldToViewportPoint(theObj.transform.position);

and aplying the resuling vector to the sprites transform.localPosition. (since it is a child of the camera displaying it)
But i always get results far beyond the view of my uiCamera.
I dont think this is a problem but i will tell you nevertheless: both cameras are at distinct positions not overlapping each other. SO the uiCamera sees nothing of the scene whereas the sceneCamera sees nothing of the ui.

My question would be: how does i properly place my sprite? Is there a completely other way i must go?

EDIT:
I might add that the gui camera is orthographic and the scene camera is set to be perspective
« Last Edit: July 08, 2014, 10:40:54 AM by zetain »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Placing GUI elements based on object in scene
« Reply #1 on: July 08, 2014, 12:37:26 PM »
A single transform is not enough. You need to transform from 3D world space to main camera's screenspace (mainCam.WorldToScreenPoint). After that you need to transfer from screen space to UI camera's world space (uiCam.ScreenToWorldPoint). Now you need to inverse transform this point by your widget's parent: (widget.transform.parent.InverseTransformPoint). Now you can set the widget's local position to this value.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Placing GUI elements based on object in scene
« Reply #2 on: July 08, 2014, 12:57:23 PM »
Here's the full function, for reference:
  1.         /// <summary>
  2.         /// Convert the specified world point from one camera's world space to another, then make it relative to the specified transform.
  3.         /// You should use this function if you want to position a widget using some 3D point in space.
  4.         /// Pass your main camera for the "worldCam", and your UI camera for "uiCam", then the widget's transform for "relativeTo".
  5.         /// You can then assign the widget's localPosition to the returned value.
  6.         /// </summary>
  7.  
  8.         static public Vector3 WorldToLocalPoint (Vector3 worldPos, Camera worldCam, Camera uiCam, Transform relativeTo)
  9.         {
  10.                 worldPos = worldCam.WorldToViewportPoint(worldPos);
  11.                 worldPos = uiCam.ViewportToWorldPoint(worldPos);
  12.                 if (relativeTo == null) return worldPos;
  13.                 relativeTo = relativeTo.parent;
  14.                 if (relativeTo == null) return worldPos;
  15.                 return relativeTo.InverseTransformPoint(worldPos);
  16.         }

zetain

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 8
    • View Profile
Re: Placing GUI elements based on object in scene
« Reply #3 on: July 09, 2014, 03:29:21 AM »
Thank you very much. that works well. I had to set the z value to some lower value so the uicamera shows it, though.
I would have needed ages to come to this solution. You saved me hours of work (or ages).

Is there some indepth tutorial about this topic somewhere? I searched the internet but found aside from unity documentation (which is anything but indepth) none.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Placing GUI elements based on object in scene
« Reply #4 on: July 09, 2014, 05:03:52 PM »
If there is, I'm not aware of any. I had to figure it out on my own at some point.