2D Toolkit Forum

2D Toolkit => Support => Topic started by: kalid360 on October 30, 2013, 06:48:56 pm

Title: Clamping Camera based on viewport
Post by: kalid360 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!
Title: Re: Clamping Camera based on viewport
Post by: unikronsoftware 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.
Title: Re: Clamping Camera based on viewport
Post by: kalid360 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.
Title: Re: Clamping Camera based on viewport
Post by: unikronsoftware 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.
Title: Re: Clamping Camera based on viewport
Post by: kalid360 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?
Title: Re: Clamping Camera based on viewport
Post by: unikronsoftware 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.
Title: Re: Clamping Camera based on viewport
Post by: kalid360 on October 31, 2013, 03:03:57 pm
it works!
Thanks a lot!