- Home /
Camera only panning on 1 axis
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);
}
}
}
}
}
Unless there is a better way to do camera panning that i am missing?
Just glanced a the code, noticed something:
Vector3 move2 = new Vector3 (pos2.z * dragSpeed, 0, 0);
I think should be
Vector3 move2 = new Vector3 (0, 0, pos2.z * dragSpeed);
At line 103
And line 99 is duplicate of line 83 not that it matter much
Ah yes i see that mistake on line 103, making the changes still dosn't solve the problem i am having though unfortunately, Thanks for pointing that out though, Can you see anything else? ha
Hmm what are the rotations of your camera? Perhaps you have y as "up"
Try Vector3 move2 = new Vector3 (0, pos2.z * dragSpeed, 0);
If your background, ground game object are like they usually are y becomes up for me. In top of scene set Global to Local to the transforms direction change when you rotate.
Answer by Scribe · Nov 05, 2014 at 01:05 PM
I think there are much better ways of doing this, here's a quick example of how I might go about it:
public float dragSpeed = 2;
Vector3 dragOrigin;
public bool cameraDragging = false;
public float outerLeft = -1000f;
public float outerRight = 1000f;
public float outerDown = -1000f;
public float outerUp = 1000f;
public float zoomSpeed = 100;
Vector3 dir;
Camera cam;
Transform trans;
void Start(){
cam = this.camera;
trans = this.transform;
Clamp();
}
void Update(){
if(Input.GetKey(KeyCode.KeypadPlus) || Input.GetAxis("Mouse ScrollWheel") > 0) {
cam.orthographicSize -= zoomSpeed;
outerLeft -= zoomSpeed;
outerRight += zoomSpeed;
outerDown -= zoomSpeed;
outerUp += zoomSpeed;
Clamp();
}else if(Input.GetKey(KeyCode.KeypadMinus) || Input.GetAxis("Mouse ScrollWheel") < 0) {
cam.orthographicSize += zoomSpeed;
outerLeft += zoomSpeed;
outerRight -= zoomSpeed;
outerDown += zoomSpeed;
outerUp -= zoomSpeed;
Clamp();
}
if(Input.GetMouseButtonDown(1)){
dragOrigin = Input.mousePosition;
cameraDragging = true;
}
if(Input.GetMouseButtonUp(1)){
cameraDragging = false;
}
if(cameraDragging){
dir = cam.ScreenToViewportPoint(Input.mousePosition - dragOrigin);
Vector3 pos = trans.position;
pos += (trans.up*dir.y + trans.right*dir.x)*dragSpeed;
pos.x = Mathf.Clamp(pos.x, outerLeft, outerRight);
pos.z = Mathf.Clamp(pos.z, outerDown, outerUp);
trans.position = pos;
}
}
void Clamp(){
cam.orthographicSize = Mathf.Clamp(cam.orthographicSize, 1, 20000);
outerLeft = Mathf.Clamp(outerLeft, -19800, 0);
outerRight = Mathf.Clamp(outerRight, 0, 19800);
outerDown = Mathf.Clamp(outerDown, -19800, 0);
outerUp = Mathf.Clamp(outerUp, 0, 19800);
}
Hope that helps!
Scribe
Nice looks much cleaner than $$anonymous$$e, Yet it still does not do what im intending to do, It moves along the X axis just fine, But it dosnt move along the Z axis to go up and down, Bare in $$anonymous$$d the camera is in top down rotation 90,0,0, Thats the problem im trying to solve, Thanks.
Edited the code slightly, should work for any camera orientation now!
Nice good job, I also fixed my script too, I am still pretty new to this stuff and you writing up a better version has given me ideas of how to structure things, Thanks.
By the way if you zoom in all the way to the $$anonymous$$ orthographicSize and move the camera to one of the corners, then zoom out, it dose not reset back to the set borders, it will just zoom out in place, was that intended? Thanks again.
not intended but easy to fix, add the following inside the clamp method:
Vector3 pos = trans.position;
pos.x = $$anonymous$$athf.Clamp(pos.x, outerLeft, outerRight);
pos.z = $$anonymous$$athf.Clamp(pos.z, outerDown, outerUp);
trans.position = pos;
That should fix it! Glad you found it useful :)