- Home /
Clamping a 2D camera while zooming.
I am making a 2D game where the player is able to move the camera around by clicking and dragging with the mouse as well as zoom in and out using the mouse wheel. I would like to make it so that player cannot see outside the edges of the gameplay area no matter how zoomed they are. In order to do this, I have tried setting it up so that if the player is zoomed all the way out, they will not be able to move the camera at all, but the further the camera is zoomed in, the move they can move the camera.
However, I my lerp setup doesn't seem to be working the way I intended it to and I'm not sure where my error is. It currently does not change the value of the clamp at all and just sets the clamp to whatever my cameraMin and cameraMax values are.
So I am looking for why my code doesn't seem to function as intended or if anyone knows a better way to go about this issue. Here is the code I have as of now:
public var orthoZoomSpeed : float = 1f; // The rate of change of the orthographic size in orthographic mode.
var cameraMin : float;
var cameraMax : float;
var dragOrigin : Vector3;
var dragSpeed : float;
private var timeDragStarted : float;
var previousPosition : Vector3;
function Update ()
{
if (Input.GetMouseButtonDown(0))
{
//Drag start
timeDragStarted = Time.time;
dragSpeed = 0f;
previousPosition = Input.mousePosition;
}
//Calculate time difference in order for the following code NOT to run on single taps
else if (Input.GetMouseButton(0) && Time.time - timeDragStarted > 0.05f)
{
//Find the delta of this point with the previous frame
var input = Input.mousePosition;
var deltaX = (previousPosition.x - input.x) * dragSpeed;
var deltaY = (previousPosition.y - input.y) * dragSpeed;
**//THIS IS THE LERP/CLAMP SECTION I AM HAVING TROUBLE WITH///////
//Sets variables to deterine how far the camera is zoomed which
//interpolates the distance it is clamped on the x and y axis
var currentOrth : float = camera.orthographicSize;
var clampMinX : float = Mathf.Lerp(cameraMax, cameraMin, currentOrth);
var clampMaxX : float = Mathf.Lerp(cameraMin, cameraMax, currentOrth);
var clampMinY : float = Mathf.Lerp(cameraMax, cameraMin, currentOrth);
var clampMaxY : float = Mathf.Lerp(cameraMin, cameraMax, currentOrth);
//Clamp the values to limit drag
var newX = Mathf.Clamp(transform.position.x + deltaX, clampMinX, clampMaxX);
var newY = Mathf.Clamp(transform.position.y + deltaY, clampMinY, clampMaxY);**
//Move Camera
transform.position = new Vector3(newX, newY, transform.position.z);
previousPosition = input;
//Some small acceleration
if(dragSpeed < 0.1f) dragSpeed += 0.2f;
}
}
//ScrollWheel to zoom in and out.
if( Input.GetAxis("Mouse ScrollWheel") > 0 && camera.orthographicSize > cameraMin)
{
var mousePos = camera.ScreenToWorldPoint(Vector3 (Input.mousePosition.x, Input.mousePosition.y, camera.transform.position.z));
var mouseOrigin = camera.ScreenToWorldPoint(Vector3 (Screen.width / 2, Screen.height / 2, camera.transform.position.z));
transform.position.y = transform.position.y + ((mousePos.y - mouseOrigin.y) / (camera.orthographicSize * 2));
transform.position.x = transform.position.x + ((mousePos.x - mouseOrigin.x) / (camera.orthographicSize * 2));
transform.position.z = -15;
camera.orthographicSize -= .5 * orthoZoomSpeed;
camera.orthographicSize = Mathf.Clamp(camera.orthographicSize, cameraMin, cameraMax);
}
else if(Input.GetAxis("Mouse ScrollWheel") < 0 && camera.orthographicSize < cameraMax)
{
mousePos = camera.ScreenToWorldPoint(Vector3 (Input.mousePosition.x, Input.mousePosition.y, camera.transform.position.z));
mouseOrigin = camera.ScreenToWorldPoint(Vector3 (Screen.width / 2, Screen.height / 2, camera.transform.position.z));
transform.position.y = transform.position.y - ((mousePos.y - mouseOrigin.y) / (camera.orthographicSize * 2));
transform.position.x = transform.position.x - ((mousePos.x - mouseOrigin.x) / (camera.orthographicSize * 2));
transform.position.z = -15;
camera.orthographicSize += .5 * orthoZoomSpeed;
camera.orthographicSize = Mathf.Clamp(camera.orthographicSize, cameraMin, cameraMax);
}
mousePos = camera.ScreenToWorldPoint(Vector3 (Input.mousePosition.x, Input.mousePosition.y, 0 - camera.transform.position.z));
mouseOrigin = camera.transform.position;
}
Thanks for any help!
Your answer
Follow this Question
Related Questions
Zooming camera using move z position. 0 Answers
camera zoom 1 Answer
How to zoom in smoothly with orthographic camera? 1 Answer
How to zoom camera in x amount of seconds 1 Answer
Camera zoom smoothing 1 Answer