Author Topic: Translating from gameplay camera coords to the UI camera  (Read 8026 times)

Harabeck

  • Guest
Translating from gameplay camera coords to the UI camera
« on: April 20, 2012, 09:37:32 PM »
In our game the player target enemies. I want an icon to show up over them when clicked, but I'm having trouble getting the icon to line up correctly.

This code gets the icon close to right screen position, but it's a bit off and too far away. (transform belongs to the gameObject with the UISprite)
  1. Vector3 pos=target.transform.position;
  2. pos=mainCam.WorldToScreenPoint(pos);
  3. pos=uiCamera.ScreenToWorldPoint(pos);
  4. transform.localPosition=pos;

Trying to bring the icon closer in just exaggerates the error in the icon's position:
  1. Vector3 relativePos=pos-mainCam.transform.position;
  2. Vector3 worldPos=mainCam.transform.position+relativePos;
  3. Vector3 mainPos=mainCam.WorldToViewportPoint(worldPos);
  4. Vector3 uiPos=uiCamera.ViewportToWorldPoint(mainPos);
  5. transform.localPosition=new Vector3(uiPos.x, uiPos.y,0);

Any idea on how to get this right?


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Translating from gameplay camera coords to the UI camera
« Reply #1 on: April 20, 2012, 09:39:04 PM »
Quote
pos=uiCamera.ScreenToWorldPoint(pos);
transform.localPosition=pos;

You're transforming to world position, but assigning the local position. You should be assigning the world position (.position)

Harabeck

  • Guest
Re: Translating from gameplay camera coords to the UI camera
« Reply #2 on: April 20, 2012, 09:47:44 PM »
That sets the icon absurdly far away (-4944, -2933, 1009188).

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Translating from gameplay camera coords to the UI camera
« Reply #3 on: April 20, 2012, 09:49:50 PM »
Well, no reason why that wouldn't work -- the first section anyway. Make sure you're using the right cameras. Also why screen pos? I suggest view pos.

Harabeck

  • Guest
Re: Translating from gameplay camera coords to the UI camera
« Reply #4 on: April 20, 2012, 10:01:33 PM »
Changing to viewport yields essentially identical results:

  1. Vector3 pos=target.transform.position;
  2. pos=mainCam.WorldToViewportPoint(pos);
  3. pos=uiCamera.ViewportToWorldPoint(pos);
  4. transform.position=pos;

And I checked with some debug messages, and the cameras are correct. The large numbers for the coordinates makes me think the UI's scale is coming into play. And even if I fix that, it's trying to put the icon at the same distance as the target, which could easily be beyond the range of the UI camera. I'd rather it appear at a uniform distance and let me control how far away it looks by scaling it.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Translating from gameplay camera coords to the UI camera
« Reply #5 on: April 20, 2012, 10:03:45 PM »
Hold on... scaling it? Are you using a pair of 3D cameras here? Why not have a 2D camera for the UI?

Did you see this post? http://www.tasharen.com/?topic=creating-a-ui-sprite-on-the-fly

Harabeck

  • Guest
Re: Translating from gameplay camera coords to the UI camera
« Reply #6 on: April 20, 2012, 10:13:31 PM »
The GUI is 3D, yes. It helps with creating a sense of depth, as though you're actually looking at a panel. It also makes the 3D radar display much more attractive. I've had no big problems until trying to get this to work. It really seems like there should be a solution.

Did you see this post? http://www.tasharen.com/?topic=creating-a-ui-sprite-on-the-fly

I hadn't, but I don't think it helps.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Translating from gameplay camera coords to the UI camera
« Reply #7 on: April 20, 2012, 10:17:20 PM »
The post I linked has a pair of scripts in it I've used to convert positions from one camera to another in order to create health bars above the character's head.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Translating from gameplay camera coords to the UI camera
« Reply #8 on: April 20, 2012, 10:25:14 PM »
This is the code I used in the Space Game Starter Kit:

  1. if (mLastTarget != null && mMainCamTrans != null)
  2. {
  3.         // Convert to view space
  4.         Vector3 pos = mLastTarget.position;
  5.         Vector3 vt = mMainCam.WorldToViewportPoint(pos);
  6.  
  7.         if (vt.z < 0f)
  8.         {
  9.                 // The target is behind the screen -- make it invisible
  10.                 mAlpha = 0f;
  11.         }
  12.         else
  13.         {
  14.                 float dist = (pos - mMainCamTrans.position).magnitude;
  15.                 mAlpha = (range.x < dist && dist < range.y) ? 1f : 0f;
  16.  
  17.                 // Clamp to the edges of the screen
  18.                 vt.x = Mathf.Clamp01(vt.x);
  19.                 vt.y = Mathf.Clamp01(vt.y);
  20.                 vt.z = dist;
  21.  
  22.                 // Convert back to world
  23.                 pos = hudCamera.ViewportToWorldPoint(vt);
  24.                 if (pos != mTrans.position) mTrans.position = pos;
  25.         }
  26. }
  27. else mAlpha = 0f;

francofresco

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: Translating from gameplay camera coords to the UI camera
« Reply #9 on: March 18, 2013, 08:31:22 PM »
Hello NGUI Authors, Devs and all others in the Community!  My first post here, but I wanted to say hello. :) I've been working with paid NGUI for a couple of months and wanted to share how much this thread helped. 

ArenMooks clamping code at the end worked perfectly. 

I also want to share, that I ended up creating a separate camera in the hierarchy under the UICamera that is smaller than the main UI camera.  I can now clamp to that camera instead of my main UICamera and have full control.  The new Camera is Ortho that doesn't clear and culls nothing.  This is only used to have more control of where the icons get clamped to as I can scale and position the camera where ever I want to in the UI Camera..

Hope this helps anyone!  NGUI FTW!  Thank you.