Help with middle mouse panning / dragging
Hey everyone, I need some help with inverse mouse panning. I need the Camera to be translated in the opposite direction the mouse is moved in, but the mouse should still be hovering over the same location as it previously was. The game is played in perspective, but I need it to anchor on z = 0 (so basically take the camera's z coordinate as the distance). However I'm having a bit of trouble getting it to work properly, the way it is now, it infinitely pans instead of keeping the mouse 'locked' in the game world. I did get it working without using Camera.main.ScreenToWorldPoint, but the mouse wasn't anchored properly and so I figured I needed to get world space involved to keep things properly synced.
// Move the camera using panning if mouse click
var mousePos = Input.mousePosition;
mousePos.z = this.transform.position.z;
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
if (isPanning) {
var translation = ((Vector2)mousePos - panOrigin);
this.transform.Translate(translation);
}
else if (Input.GetMouseButtonDown(2)) {
isPanning = true;
}
if (Input.GetMouseButtonUp(2))
isPanning = false;
else
panOrigin = mousePos;
There's a gif of something close to what I want, but that is not using world space and uses a multiplier on the distance the mouse travelled since the previous frame. This will never be exact and will be annoying to work with, but it demonstrates what I want. I want the mouse to essentially be in sync so that it's always over the door if I started panning from there