Movement and zoom of camera on the map
Hello, I'm trying to write a small 2d strategy, and I want to realize the movement of the camera with the mouse I have a pre-drawn map, which has a size 210x130 in Unity. I have a GameObject attachment with the camera, I move the GameObject the following script: using UnityEngine; using System.Collections;
public class MoveCamera : MonoBehaviour {
public float leftRestriction = -40f;
public float rightRestriction = 10f;
public float upRestriction = 30f;
public float downRestriction = -10f;
// Use this for initialization
public int theScreenWidth;
public int theScreenHeight;
public int Boundary = -10;
public int speed = 10;
// Use this for initialization
void Start ()
{
theScreenWidth = Screen.width;
theScreenHeight = Screen.height;
}
// Update is called once per frame
void Update ()
{
// Right
if (Input.mousePosition.x > theScreenWidth - Boundary && transform.position.x <= rightRestriction)
{
float posX = transform.position.x;
posX += speed * Time.deltaTime;
transform.position = new Vector3(posX, transform.position.y, transform.position.z);
}
//Left
if (Input.mousePosition.x < 0 + Boundary && transform.position.x >= leftRestriction)
{
float posX = transform.position.x;
posX -= speed * Time.deltaTime;
transform.position = new Vector3(posX, transform.position.y, transform.position.z);
}
//Up
if (Input.mousePosition.y > theScreenHeight - Boundary && transform.position.y <= upRestriction)
{
float posY = transform.position.y;
posY += speed * Time.deltaTime;
transform.position = new Vector3(transform.position.x, posY, transform.position.z);
}
//Down
if (Input.mousePosition.y < 0 + Boundary && transform.position.y >= downRestriction)
{
float posY = transform.position.y;
posY -= speed * Time.deltaTime;
transform.position = new Vector3(transform.position.x, posY, transform.position.z);
}
}
}
Everything works fine but if I change the size of the camera, I can see the blue area. I worked with a zoom camera like this:
using UnityEngine;
using System.Collections;
public class ZoomCamera : MonoBehaviour {
public float zoomSpeed = 1;
public float targetOrtho;
public float smoothSpeed = 2.0f;
public float minOrtho = 1.0f;
public float maxOrtho = 20.0f;
// Use this for initialization
void Start()
{
targetOrtho = Camera.main.orthographicSize;
}
// Update is called once per frame
public float lastOrtho;
void Update()
{
float scroll = Input.GetAxis("Mouse ScrollWheel");
if (scroll != 0.0f)
{
targetOrtho -= scroll * zoomSpeed;
targetOrtho = Mathf.Clamp(targetOrtho, minOrtho, maxOrtho);
}
Camera.main.orthographicSize = Mathf.MoveTowards(Camera.main.orthographicSize, targetOrtho, smoothSpeed * Time.deltaTime);
}
public GameObject MoveCamera;
}
Could you tell me how to do so that when i change the size there is not the blue area. Maybe it is necessary to shift the camera with certain conditions, but I don't understand what conditions and how. I beg your help
Comment