- Home /
Fixing object position within range
Hi,
I'm working on a feature where you can mouse-pan the camera across a scene. The idea is that you cannot pan outside of the scene. There are currently 2 issues:
1) You can still pan along the x-direction and go outside the bounds (meaning the bounds are calculated incorrectly)
2) If I resize the screen, the bounds are the wrong size.
My logic for boundaries was this: if the next pan goes outside the boundary, cancel.
Here's my code, I don't know where I pulled the number 5 out of but it worked for that current screen size, and that's the problem. But I don't know what the calculation should be.
using UnityEngine;
using System.Collections;
public class ZoomPan : MonoBehaviour
{
public float moveSensitivityX = 1.0f;
public float moveSensitivityY = 2.0f;
public bool updateZoomSensitivity = false;
public float orthoZoomSpeed = 1.0f;
public float minZoom = 10.0f;
public float maxZoom = 40.0f;
public bool invertMoveX = false;
public bool invertMoveY = false;
public float zoomMultiplier = 1f;
private bool isPanning = false;
private float boundaryBottom;
private float boundaryTop;
private float boundaryLeft;
private float boundaryRight;
void Start()
{
maxZoom = 80f / Screen.width * Screen.height / 2.0f;
Camera.main.orthographicSize = maxZoom;
boundaryBottom = Camera.main.transform.position.y - 5;
boundaryTop = Camera.main.transform.position.y + 5;
boundaryLeft = Camera.main.transform.position.x - 5;
boundaryRight = Camera.main.transform.position.x + 5;
}
private Vector2 startPanPos;
void Update()
{
//checkNotOutsideBounds();
if(updateZoomSensitivity)
{
moveSensitivityX = Camera.main.orthographicSize / 10.0f;
moveSensitivityY = Camera.main.orthographicSize / 5.0f;
}
if(!isPanning && Input.GetMouseButton(0)) //start panning
{
//Debug.Log("Panning");
isPanning = true;
startPanPos = Input.mousePosition;
}
else if(isPanning && !Input.GetMouseButton(0)) //stop panning
{
//Debug.Log("Not panning");
isPanning = false;
}
else if(isPanning && Input.GetMouseButton(0)) //panning
{
Vector2 delta = new Vector2(Input.mousePosition.x - startPanPos.x, Input.mousePosition.y - startPanPos.y);
float posX = delta.x * moveSensitivityX * zoomMultiplier * Time.deltaTime;
posX = invertMoveX ? posX : -posX;
float posY = delta.y * moveSensitivityY * zoomMultiplier * Time.deltaTime;
posY = invertMoveY ? posY : -posY;
Debug.Log(Camera.main.transform.position.x + "," + Camera.main.transform.position.y + "," + Camera.main.transform.position.z + " --> clamped within " + boundaryBottom + "|" + boundaryTop + ", " + boundaryLeft + "|" + boundaryRight);
Vector3 newPosition = Camera.main.transform.position + new Vector3(posX, posY, posX);
bool canPan = true;
if(zoomMultiplier == 1f)
{
if(Screen.width > Screen.height)
{
posY = 0; //clamp y-movement
}
else
{
posX = 0; //clamp x-movement
}
if(newPosition.x < boundaryLeft || newPosition.x > boundaryRight)
canPan = false;
if(newPosition.y < boundaryBottom || newPosition.y > boundaryTop)
canPan = false;
}
if(canPan)
Camera.main.transform.position = newPosition;
startPanPos = Input.mousePosition;
}
else if(Input.GetAxis("Mouse ScrollWheel") > 0f) //zoom in
{
//Debug.Log("Zoom in");
Camera.main.orthographicSize -= orthoZoomSpeed;
Camera.main.orthographicSize = Mathf.Clamp(Camera.main.orthographicSize, minZoom, maxZoom);
zoomMultiplier = Camera.main.orthographicSize / maxZoom;
}
else if(Input.GetAxis("Mouse ScrollWheel") < 0f) //zoom out
{
//Debug.Log("Zoom out");
Camera.main.orthographicSize += orthoZoomSpeed;
Camera.main.orthographicSize = Mathf.Clamp(Camera.main.orthographicSize, minZoom, maxZoom);
zoomMultiplier = Camera.main.orthographicSize / maxZoom;
}
}
}
In the image, you can see both issues. I cannot pan any further up (there's a margin of the same size around the whole of the checkered square), and also the boundary has been incorrectly calculated on the x-axis. If my screen was larger in height than width, I would assume the boundary on the y-axis would be incorrectly calculated instead.
As a side-note, when the scene is fully-zoomed-out, zoom$$anonymous$$ultiplier is 1 and goes down to 0.25 as the furthest zoomed in
Your answer
![](https://koobas.hobune.stream/wayback/20220612075848im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Zooming in and out with an orthographic camera, while the bottom edge is fixed. 0 Answers
Clamping local position 0 Answers
Clamping a Manipulated Camera 0 Answers