Author Topic: Clamp orthographic camera to map bounds  (Read 4771 times)

technoir

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 1
  • Posts: 27
    • View Profile
Clamp orthographic camera to map bounds
« on: July 25, 2014, 12:32:40 PM »
Hi,

I'm trying to write a script that creates clamps for the X and Y coordinates of an orthographic camera when panning around a map (UISprite). I am trying to set it up so that the camera bounds never leave the bounds of the map (i.e. so no background is shown). The Size of the orthographic camera will also change as I plan to integrate zooming later on. I have adapted a script that many people use for this purpose but am having some trouble getting it to work properly.

As the Size of the map is stored in the UISprite component and not in world coordinates, it is clamping at the wrong point. The clamp on the X stops just under half the width of the camera over the map bounds, and the Y stops at the camera's centre (so half in, half out). To clarify, in order to work correctly the bounds of the camera should stop at the bounds of the map, no matter its size.

I have pasted the script below. The Size of the map in its UPSprite is 1280 x 800, the size of the camera is 1 (but will change, so should not make a difference).

Any help would be fantastic as this one has stumped me! :)

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class ChangeMapSize : MonoBehaviour {
  5.  
  6.     private float minX;
  7.     private float maxX;
  8.     private float minY;
  9.     private float maxY;
  10.     private float mapW;
  11.     private float mapH;
  12.     private Transform Map;
  13.  
  14.     void Start()
  15.     {
  16.         // Reference to the map transform
  17.         Map = GameObject.FindGameObjectWithTag(Tags.Map).transform;
  18.  
  19.         // Access public static members to retrieve the map size
  20.         mapW = FindMapSize.MapWidth;  // 1280
  21.         mapH = FindMapSize.MapHeight; // 800
  22.  
  23.         // Calculate the vertical size of the camera
  24.         float vertExtent = transform.camera.orthographicSize;
  25.  
  26.         // Calculate the horizontal size of the camera in screen space
  27.         float horzExtent = vertExtent * Screen.width / Screen.height;
  28.  
  29.         // Calculations assume map is position at the origin
  30.         minX = horzExtent - mapH / 2.0f;
  31.         maxX = mapH / 2.0f - horzExtent;
  32.         minY = vertExtent - mapH / 2.0f;
  33.         maxY = mapH / 2.0f - vertExtent;
  34.     }
  35.  
  36.     void LateUpdate()
  37.     {
  38.         Vector3 v3 = transform.localPosition;
  39.         v3.x = Mathf.Clamp(v3.x, minX, maxX);
  40.         v3.y = Mathf.Clamp(v3.y, minY, maxY);
  41.         transform.localPosition = v3;
  42.     }
  43. }
  44.  
  45.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Clamp orthographic camera to map bounds
« Reply #1 on: July 26, 2014, 03:22:56 AM »
I suggest looking at the Example 8 that comes with NGUI. It constrains the camera using the bounds of the scroll view's content, which is similar to what you're trying to do.

First it calculates the bounds of the content on line 231 of UIDraggableCamera.cs. It then calls the ConstrainToBounds function, which in turn calls CalculateConstrainOffset() that does all the math inside.

technoir

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 1
  • Posts: 27
    • View Profile
Re: Clamp orthographic camera to map bounds
« Reply #2 on: July 29, 2014, 06:45:25 PM »
Thanks, it looks promising - I'll take a look! :)