- Home /
Map Panning
I have created a map system within my game. My plan is to allow the map to pan in the style of Google Maps. Click and drag, rest when not moving.
The problem I am having is that everything works wonderfully for the first drag one does, however on the second drag, the camera will poof directly to the 0,0 position before continuing on its drag again. This means that it is impossible to properly pan the camera past a small area.
I am using this script, attached to the camera, to move the camera according to the location of the map:
deltaX = (pos.x-map.transform.position.x) - oldX;
deltaY = (pos.y-map.transform.position.y) - oldY;
oldX = (transform.position.x-map.transform.position.x);
oldY = (transform.position.y-map.transform.position.y);
if (Input.GetMouseButton(0) || (Input.touchCount >= 1 && Input.GetTouch(0).phase == TouchPhase.Moved))
{
pos = Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, 1));
transform.position.x = (pos.x-map.transform.position.x)-deltaX;
transform.position.y = (pos.y-map.transform.position.y)-deltaY;
}
The map itself is initially set to the position of a GPS reading. If, for example, you are in France, the map will move down and to the left to set the france position of the map to the 0,0 point in the stage. After this, everything is static but the camera, which seems unable to properly move.
Any help would be greatly appreciated.
Answer by GrahamReeves · Apr 15, 2014 at 04:02 PM
It might be less confusing if you have less delta, old and new position persistent variables. This script attached to the camera I believe does what you want, call OnCameraMoved() when you've moved the map (In hindsight I should have called that, OnMapMoved()).
I've changed the code around so you're explicitly handling a single drag at a time instead of storing loads of positions.
public class PanWithMouse : MonoBehaviour {
public Vector3 dragStart;
public bool wasDown = false;
public Vector3 cameraOffset = new Vector3(10,10,10);
void Start ()
{
OnCameraMoved();
}
void OnCameraMoved()
{
GameObject map = GameObject.Find ("Map");
if (map)
{
cameraOffset = transform.position - map.transform.position;
}
}
void Update () {
GameObject map = GameObject.Find ("Map");
if (!map) {
Debug.Log ("Could not find map");
return;
}
bool NowDown = Input.GetMouseButton (0);
if (!wasDown && NowDown)
{
Debug.Log("start drag");
dragStart = Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 1));
}
else if (wasDown && NowDown)
{
Vector3 DragPos = Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 1));
Vector3 Delta = dragStart - DragPos;
Debug.Log( Delta );
cameraOffset += Delta;
}
wasDown = NowDown;
transform.position = map.transform.position + cameraOffset;
}
}
Answer by pedro_andre_oliveira · May 17, 2015 at 01:21 PM
Hello,
I'm fairly new to location-based games. I am working on a location-based game that requires access to a map; I am downloading it via WWW(url) from Google Maps (as in the example unity package) to a RAW Imagem texture field.
Is this the right way? How is pan achieved? I was thinking of having a large-scale map and zooming in by moving the camera; panning the same way. Is this the correct way?
The idea is contrary to downloading a new texture everytime I need to change the map position which requires more bandwidth.
I am also trying to implement this uncoupled of the web service, or, working with Google, Bing and Open Street Maps.
Any help is welcomed!
Your answer
![](https://koobas.hobune.stream/wayback/20220613142747im_/https://answers.unity.com/themes/thub/images/avi.jpg)