Help with Inverse Mouse Panning (Middle mouse drag
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;
Your answer
Follow this Question
Related Questions
Rotate towards mouse pointer 1 Answer
Needing help with 3rd person mouse camera 1 Answer
drag AND drop push and pull object 0 Answers
Simple Camera Switch Using C# 3 Answers
How to get photo image and work with it 0 Answers