Currently I have an image on screen ,but I may change this to gameobjects in the future.
What I’m trying to do is only allow the camera to pan around the image (but stop it when I need to) this is based off Candy Crushes Level Selector, although it works I’m not to sure if there is a better way of doing this and wanted to ask others if they new.
I’m not to happy about the code in the mDrag==true section.
basically I’m trying to get the Left/right/up/down sides of the camera and not allow this to move outside + a little gap, so the user cannot scroll the camera into infinity and beyond (buzz light year)
any idea’s would be great.
public class DragCameraComponent : MonoBehaviour
{
private Vector3 Origin;
private Vector3 Difference;
private bool mIsDrag = false;
void LateUpdate()
{
if (Input.GetMouseButton(0) == true)
{
Difference = (Camera.main.ScreenToWorldPoint(Input.mousePosition)) - Camera.main.transform.position;
if (mIsDrag == false)
{
mIsDrag = true;
Origin = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
}
else
mIsDrag = false;
if (mIsDrag == true)
{
Vector3 mOldPosition = Camera.main.transform.position;
Camera.main.transform.position = Origin - Difference;
// top right
Vector3 mTopRight = Camera.main.ViewportToWorldPoint(new Vector3(1, 1, Camera.main.nearClipPlane));
// bottom left
Vector3 mBottomLeft = Camera.main.ViewportToWorldPoint(new Vector3(0, 0, Camera.main.nearClipPlane));
if ((mBottomLeft.x < -10 || mTopRight.x >= 10) || (mBottomLeft.y < -9 || mTopRight.y > 9))
{
Camera.main.transform.position = mOldPosition;
}
}
}
}
If it’s an orthographic camera, you can use Camera.orthographicSize to get half the height, and multiply that by Camera.aspect to get half the width. Then you can add/subtract these from the camera’s position to get the position of the borders. After that you can use whatever method to limit the movement of the camera.
If device aspect ratio doesn’t vary much, you might be able to get away with just clamping the position of the camera, but I’m guessing you’re concerned about this. Am I understanding correctly?
Hi Mate,
Not hurd from you in a while, did you check out the images I left you? hope the wife is ok and drawing like mad 
I’ve seen an example of that here, bummer forgot to save it, if I remember its
Height = 2 * Othergraphicsize
Width = Height * Othergraphicsize.aspect.
is that right I only glanced at the page??
She’s too busy with school, sadly, but she’ll find time eventually, I’m sure. I don’t remember pics, let me check again…
orthographic, not Othergraphic! But other than that, yes. Bonne création.
I like to use the physics engine to clamp my camera. It allows me to easily change borders, define custom bounding shapes, path panning, elastic panning, control the behavior of the camera pan etc… I just put a RigidBody2D with no gravity and a Collider2D on my camera object. Then I put colliders around the scene where I want the camera to stop. I generally use a BoxCollider2D and in my Start method I set the bounds of the collider.
using UnityEngine;
using System.Collections;
public class CameraClampPanBehavior : MonoBehaviour {
Camera cam;
Vector2 deltaPosition = Vector2.zero;
public float sensitivity = -0.2f;
public AnimationCurve easeOutCurve;
void Start()
{
cam = gameObject.GetComponent<Camera>();
BoxCollider2D boxCollider = gameObject.GetComponent<BoxCollider2D>();
float height = cam.orthographicSize * 2;
float width = height * cam.aspect;
boxCollider.size = new Vector2(width, height);
}
void Update()
{
if (Input.GetMouseButton(0))
{
deltaPosition.y = Input.GetAxis("Mouse Y");
deltaPosition.x = Input.GetAxis("Mouse X");
}
if (Input.GetMouseButtonUp(0))
{
StartCoroutine(LerpToZero());
}
}
void FixedUpdate()
{
transform.Translate(deltaPosition * sensitivity);
}
IEnumerator LerpToZero ()
{
float time = 0.5f;
float i = 0.0f;
float rate = 1.0f / time;
Vector2 start = deltaPosition;
Vector2 lerp;
while (i < 1.0f)
{
i += Time.deltaTime * rate;
lerp = Vector2.Lerp(start, Vector2.zero, easeOutCurve.Evaluate(i));
deltaPosition = lerp;
yield return new WaitForEndOfFrame();
}
}
}
For completeness sake, and for anyone else this may help, I also lerp my cameras translate down to zero when I release the mouse button so I get a nice smooth falloff to resting position. By editing the easeOutCurve in the inspector you can get all sorts of cool stopping behaviors.
Just a typo my friend not to bad I though, for a quick glance.
I’ve bee putting the map together the last couple of days, changing my mind several times, but I think I have it now, until I change it again lol, Yeah I’ll have a look at this one cheers padre 
I thought I would post back here with a reworked solution, just in case other are trying to figure it out.
-
- What this new version does is lets you scroll around a sprite that is in front of the camera.
-
- It does not get stuck when you reach the borders of the image, my first version does this, you might want to further expand the area that the camera can move over, this is easy create a couple of public variables and add/subtract these to the MathF.Clamp Min/Max
-
- selector is the name(“inspector name”) of the image the camera is scrolling over you can change this to the name of your sprite in the inspector.
-
There is further optimization you could do a form of lerp to slowly stop the movement(when you take your finger off the screen), I’ve not done this here but in the future I will.
You could also have the Camera calculations in the Awake function and put these into the SelectorArea Rect so you dont need to do these in the Mathf.Clamp method.
using UnityEngine;
using System.Collections;
using System;
public class CameraComponent : MonoBehaviour
{
private Vector3 mOrigin;
private SpriteRenderer mRenderer;
private Rect mSelectorArea;
private Single mCameraHalfHeight;
private Single mCameraHalfWidth;
public Single Dragspeed = 0.2F;
void Awake()
{
GameObject mSelector = GameObject.Find("selector");
mRenderer = mSelector.GetComponent<SpriteRenderer>();
Single mWidth = mRenderer.bounds.size.x;
Single mHeight = mRenderer.bounds.size.y;
Single mLeft = mSelector.transform.position.x - (mWidth * 0.5f);
Single mTop = mSelector.transform.position.y - (mHeight * 0.5f);
mSelectorArea = new Rect(mLeft, mTop, mWidth, mHeight);
}
void Start()
{
Single mCameraHeight = (2f * Camera.main.orthographicSize);
Single mCameraWidth = mCameraHeight * Camera.main.aspect;
mCameraHalfHeight = (mCameraHeight * 0.5F);
mCameraHalfWidth = (mCameraWidth * 0.5F);
}
void LateUpdate()
{
if (Input.GetMouseButtonDown(0))
{
mOrigin = Input.mousePosition;
return;
}
if (Input.GetMouseButton(0) == false)
return;
Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mOrigin);
Vector3 move = new Vector3(pos.x * Dragspeed, pos.y * Dragspeed, 0);
transform.Translate(move, Space.World);
this.transform.position = new Vector3(Mathf.Clamp(this.transform.position.x, mSelectorArea.xMin + mCameraHalfWidth, mSelectorArea.xMax - mCameraHalfWidth), Mathf.Clamp(this.transform.position.y, mSelectorArea.yMin + mCameraHalfHeight, mSelectorArea.yMax - mCameraHalfHeight), this.transform.position.z);
}
}
Enjoy 