Author Topic: Two cameras, different resolutions.  (Read 2847 times)

Scathis

  • Guest
Two cameras, different resolutions.
« on: January 22, 2013, 07:13:37 PM »
This is my goal: I'm trying to get health bars to show up in the right place.
What I have:
1 Main Camera used for the world. It's a perspective cam that can be dragged around.
1 UI Camera, ortho, child of UI Root
UI Root Is setup with Automatic on, Manual/minimum/maximum height to 768

What I'm doing: I'm using the main camera "WorldToScreenPoint" to find out the health bar screen coords. Then I create a child health bar, parent it to a panel that is anchored to the top left of my UI and positioning it using the Vector3 I get back from WorldToScreenPoint.

It works fine on an iPad 2/Mini but when you go to an iPad 4 with the higher resolution, that calculation is off and the bar shows up in the wrong place.

Now, I can take the screen resolution of the main cam, figure out the percentages in X and Y and translate that to the UI cam... but it seems like there should be a better way.

Is there a better way?

Thanks in advance.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Two cameras, different resolutions.
« Reply #1 on: January 22, 2013, 09:11:38 PM »
HUDText extension comes with a script that makes this easy -- UIFollowTarget. It will do exactly that for you -- have some UI component follow a 3D object's position. It's all just math. I'm guessing you are forgetting to take UIRoot's pixelSizeAdjustment into account.

Scathis

  • Guest
Re: Two cameras, different resolutions.
« Reply #2 on: January 23, 2013, 12:35:01 PM »
Figured you had something like that. Somehow I couldn't find it.
Thanks!

By the way, long time professional game dev and NGUI is the best UI system I've used. Nice work.

Scathis

  • Guest
Re: Two cameras, different resolutions.
« Reply #3 on: January 23, 2013, 02:25:24 PM »
So now I've downloaded and installed HUDText with the UIFollowTarget script.
It's not showing up in the right position.

I've debugged it to find the issue in UIFollowTarget's Update function
  1. if (isVisible)
  2. {
  3.         transform.position = mUICamera.ViewportToWorldPoint(pos);
  4.         pos = mTrans.localPosition;
  5.         pos.x = Mathf.FloorToInt(pos.x);
  6.         pos.y = Mathf.FloorToInt(pos.y);
  7.         pos.z = 0f;
  8.         mTrans.localPosition = pos;
  9. }
  10.  

When my code runs it his the "mUICamera.ViewportToWorldPoint(pos)" and that comes back as a reasonable vector.
But when it gets the local position it's some huge vector.
Pos ends up being a vector way off my screen. Which gets me to thinking, does the UICamera have to be placed at a certain spot in the map for this to work properly?
Seems like the translation is failing because I have my UI camera off in a corner of the scene for easy editing.

Any help is appreciated.

Scathis

  • Guest
Re: Two cameras, different resolutions.
« Reply #4 on: January 23, 2013, 02:53:49 PM »
After finding the second page of the stickied heath bar thread, seems like I'm having the same issue.

Will try to fix it using the info in there and if I run into more problems, I'll post there.