Author Topic: Helper functions for moving a camera, not widgets  (Read 9399 times)

MoProductions

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 45
    • View Profile
Helper functions for moving a camera, not widgets
« on: December 19, 2016, 11:23:38 AM »
We're working on a project that moves the camera instead of the game objects.  Between Unity's Camera class and the NGUIMath/NGUITools there's lots of functions for moving game objects to be within the bounds of a camera (FitOnScreen, etc).  However, for our game, all of the game objects will be stationary, so what we need is something that isn't telling us where to put an object so that it's at a certain point within the camera's view, but we want to move the camera to the exact spot to get the object where we want it. 
For example, it's easy enough to move a widget to the exact edge of the screen using stuff like ViewpointPorttoWorldPoint(), but is there a way to calculate where we'd need to put the camera to get the widget at the exact edge of the screen?  I've been looking into bounds calculations but I'm still missing something.
BTW we're using a multiple camera setup, camera in camera sort of thing, so we'd need to be able to do this with any camera, not just one that's bounded by the screen.
Thanks!  Let me know if this needs any clarification.
-Mo

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Helper functions for moving a camera, not widgets
« Reply #1 on: December 19, 2016, 11:09:11 PM »
Just the inverse, no? Instead of moving the widget by XYZ pixels, move the camera by -XYZ pixels.

MoProductions

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 45
    • View Profile
Re: Helper functions for moving a camera, not widgets
« Reply #2 on: December 21, 2016, 12:36:20 PM »
I didn't think it could be that easy, especially when zooming is involved, but it did end up being something like that.  Here's an example of clamping the camera so that the bounds object is at the far right if it's gone too far left.

  1. Vector3 boundWTV;
  2. boundWTV = puzzleCamera.WorldToViewportPoint( upperRightBounds.bounds.max );
  3. if( boundWTV.x < 1f )
  4. {
  5.         Vector3 destVTW = puzzleCamera.ViewportToWorldPoint( new Vector3( 1f, boundWTV.y, 0f ) );      
  6.         Vector3 offset = destVTW - upperRightBounds.bounds.max;
  7.         puzzleCamera.transform.position -= offset;     
  8. }
  9.