Hello Guest

Author Topic: Clamping Camera based on viewport  (Read 5419 times)

kalid360

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 6
    • View Profile
Clamping Camera based on viewport
« on: October 30, 2013, 06:48:56 pm »
I'm having issues constraining my main camera in the game. This camera follows the character around.
I need it to clamp from the beginning to the beginning to the end of the game, resolution/aspect ratio independent.
Right now I have fixed numbers clamping the camera, which isn't good because it doesn't work well across multiple displays.

transform.position =  new Vector3(Mathf.Clamp(transform.position.x, leftOffset, rightOffset), transform.position.y, transform.position.z);

I also tried using empty game objects and that didn't work either.

I currently have a wildcard on my 2D toolkit camera (if that helps):
Match by: wildcard
Autoscale: Fit to height
Fit mode: Center

Any help would be much appreciated!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Clamping Camera based on viewport
« Reply #1 on: October 30, 2013, 07:30:08 pm »
The bounds of the camera after scaling, etc. is returned by the tk2dCamera.ScreenExtents. You can use this to decide how much to constrain the camera by.

kalid360

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Clamping Camera based on viewport
« Reply #2 on: October 30, 2013, 08:01:27 pm »
What do you mean? How would I use that on deciding how much to constrain by?
I'm a little confused.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Clamping Camera based on viewport
« Reply #3 on: October 30, 2013, 09:38:02 pm »
ScreenExtents.width gives you the 4 extents of the screen. Refer to tk2dCameraAnchor.UpdateTransform - it positions objects at the edges of the screen - you can use this these coordinates to decide how to constrain your camera, basically you want to constrain by the camera position + half the dimension of your sprite / object.

kalid360

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Clamping Camera based on viewport
« Reply #4 on: October 30, 2013, 11:07:40 pm »
Ok I took a look at that function and ScreenExtents.width again but I'm still not understanding.

When I divide my anchor transform by half and the camera transform but that doesn't give me expected results with different screen res/ratios.
I also applied the  tk2dCameraAnchor to the border, the anchoring works great but my math is still wrong.

float begin =  transform.position.x + beginBorderTransform.position.x / 2;
float end = transform.position.x + endBorderTransform.position.x / 2;

transform.position = new Vector3(Mathf.Clamp(transform.position.x, begin, end), transform.position.y,transform.position.z);

What am I missing?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Clamping Camera based on viewport
« Reply #5 on: October 31, 2013, 01:07:10 pm »
If min and max are game objects at the minimum and maximum world space positions you want to clamp the camera to, you should do something like this:
Code: [Select]
Rect r = cam.ScreenExtents;
Vector3 pos = cam.transform.position;
pos.x = Mathf.Clamp(pos.x, min.position.x - r.xMin, max.position.x - r.xMax);
pos.y = Mathf.Clamp(pos.y, min.position.y - r.yMin, max.position.y - r.yMax);
cam.transform.position = pos;
Thats it really.

kalid360

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Clamping Camera based on viewport
« Reply #6 on: October 31, 2013, 03:03:57 pm »
it works!
Thanks a lot!