Hello before i continue here is a WebPlayer Example
Zoom in with Scroll Wheel
Pan around by holding right mouse button and moving mouse.
I am trying to make a world map that can be zoomed into and panned around, I have managed to add the zooming and left to right panning but for some reason it will not pan up and down,
The camera is top down orthographic so i would need to get the X and Z correct?
Here is what i have got so far, Any pointers or hints would be greatly appreciated thanks.
using UnityEngine;
using System.Collections;
public class DragCamera : MonoBehaviour
{
public float dragSpeed = 2;
private Vector3 dragOrigin;
public bool cameraDragging = true;
public float outerLeft = -1000f;
public float outerRight = 1000f;
public float outerUp = -1000f;
public float outerDown = 1000f;
public float zoomSpeed = 1000;
Camera cam;
void Start(){
cam = this.camera;
}
void Update()
{
if (outerLeft > 0)
outerLeft = 0;
if (outerLeft < -19800)
outerLeft = -19800;
if (outerRight < 0)
outerRight = 0;
if (outerRight > 19800)
outerRight = 19800;
if (cam.orthographicSize > 20000)
cam.orthographicSize = 20000;
if (cam.orthographicSize < 200)
cam.orthographicSize = 200;
Vector3 mousePosition = new Vector3 (Input.mousePosition.x, Input.mousePosition.z);
float left = Screen.width * 0.2f;
float right = Screen.width - (Screen.width * 0.2f);
float up = Screen.height * 0.2f;
float down = Screen.height - (Screen.height * 0.2f);
if (mousePosition.x < left) {
cameraDragging = true;
}else
if(mousePosition.x > right) {
cameraDragging = true;
}
if (mousePosition.z < up) {
cameraDragging = true;
}else
if (mousePosition.z > down) {
cameraDragging = true;
}
if (Input.GetKey (KeyCode.KeypadPlus) || Input.GetAxis("Mouse ScrollWheel") > 0) {
cam.orthographicSize -= zoomSpeed;
outerLeft -= zoomSpeed;
outerRight += zoomSpeed;
}
if (Input.GetKey (KeyCode.KeypadMinus) || Input.GetAxis("Mouse ScrollWheel") < 0) {
cam.orthographicSize += zoomSpeed;
outerLeft += zoomSpeed;
outerRight -= zoomSpeed;
}
if (cameraDragging) {
if (Input.GetMouseButtonDown (1)) {
dragOrigin = Input.mousePosition;
return;
}
if (!Input.GetMouseButton (1))
return;
Vector3 pos = Camera.main.ScreenToViewportPoint (Input.mousePosition - dragOrigin);
Vector3 move = new Vector3 (pos.x * dragSpeed, 0, 0);
if (move.x > 0f) {
if (this.transform.position.x < outerRight) {
transform.Translate (move, Space.World);
}
}else{
if (this.transform.position.x > outerLeft) {
transform.Translate (move, Space.World);
}
}
if (!Input.GetMouseButton (1))
return;
Vector3 pos2 = Camera.main.ScreenToViewportPoint (Input.mousePosition - dragOrigin);
Vector3 move2 = new Vector3 (pos2.z * dragSpeed, 0, 0);
if (move2.z > 0f) {
if (this.transform.position.z < outerDown) {
transform.Translate (move2, Space.World);
}
}else{
if (this.transform.position.z > outerUp) {
transform.Translate (move2, Space.World);
}
}
}
}
}