- Home /
Keeping the camera in bounds
I have a 2D Orthographic camera. Its pointing at my background GameObject. The background has a Mesh and a MeshRenderer on it, its created by UniTMX. I would like to let the player drag the camera around but I want it to always have the background visible and covering the full screen.
This is what I'm trying... Its not really working at all. I'm guessing GameObject.Find("Background").GetComponent().sharedMesh.bounds
isn;t what I should be looking at.
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
enum Mode {Idle, Follow, Drag}
private Bounds backgroundBounds;
private float rightBound;
private float leftBound;
private float topBound;
private float bottomBound;
private Mode state = Mode.Idle;
public Transform followTarget;
private Vector3 pos;
public float dragSpeed = 2;
private Vector3 dragOrigin;
void Start ()
{
float vertExtent = Camera.main.camera.orthographicSize;
float horzExtent = vertExtent * Screen.width / Screen.height;
backgroundBounds = GameObject.Find("Background").GetComponent<MeshFilter>().sharedMesh.bounds;
leftBound = (float)(horzExtent - backgroundBounds.size.x / 2.0f);
rightBound = (float)(backgroundBounds.size.x / 2.0f - horzExtent);
bottomBound = (float)(vertExtent - backgroundBounds.size.y / 2.0f);
topBound = (float)(backgroundBounds.size.y / 2.0f - vertExtent);
Debug.Log(backgroundBounds);
}
void Update ()
{
// Switch Modes
if (Input.GetMouseButtonDown(0)) {
state = Mode.Drag;
dragOrigin = Input.mousePosition;
followTarget = null;
}
else if (!Input.GetMouseButton(0) && state == Mode.Drag) {
state = Mode.Idle;
}
else if (followTarget && state == Mode.Idle) {
state = Mode.Follow;
}
// Drag camera around
if (state == Mode.Drag) {
Vector3 mousePos = Camera.main.ScreenToViewportPoint(Input.mousePosition - dragOrigin);
Vector3 move = new Vector3(mousePos.x * dragSpeed, mousePos.y * dragSpeed, 0);
transform.Translate(move, Space.World);
}
// If we have a target to follow move to them, but stay on the background
else if (state == Mode.Follow) {
var pos = new Vector3(followTarget.position.x, followTarget.position.y, followTarget.position.z);
transform.position = pos;
}
}
void LateUpdate () {
Vector3 v3 = transform.position;
v3.x = Mathf.Clamp(v3.x, leftBound, rightBound);
v3.y = Mathf.Clamp(v3.y, bottomBound, topBound);
transform.position = v3;
}
void OnLevelWasLoaded ()
{
Start();
}
}
Answer by Jeff-Kesselman · Jun 26, 2014 at 04:58 PM
If you want the background to stay in a ixed position to the camera simply make it a child object of the camera object.
No code is needed.
(if you want a background that's bigger then the camera that you can scroll around, thats another story but then specify that in the question.)
Okay thats a bit trickier. But lets assume you have the background centered at position 0,0,0
There are a couple ways to do it. You can do it in camera space, or you can do it in screen space. I think id use screen space because its easier to find the dimensions of then the camera frustrum.
In order to find the bounds of the texture in screen space, use http://docs.unity3d.com/ScriptReference/Camera.WorldToScreenPoint.html on the four corners of the texture in world space.
If any of those points are inside the screen rectangle, it means that you have scrolled too far.
You can fix it by subtracting the texture's corner from the corner of the screen it should be in, and using http://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html to convert the delta back to a world measure to move the camera by.
Your answer
Follow this Question
Related Questions
Implementing Camera bounds proportionally to its ortographic size 0 Answers
Fitting Bounds into Orthographic 2d Camera. 2 Answers
Unity ortographic camera panning within boundaries issue 0 Answers
How do I lock an orthographic camera? 1 Answer
How do I keep an Ortho camera in a specific range when the ortho changes? 3 Answers