- Home /
Question by
BZS · Jan 11, 2014 at 06:24 PM ·
camerajavascriptorthographicmouse drag
Pan Orthographic Camera?
I have a 2d game and I want it so when the mouse button is down and I drag the mouse the camera pans with the mouse. How would I do this. I code in javascript by the way. Thanks in advanced.
Comment
Wiki
Answer by agies1 · Jan 11, 2014 at 07:04 PM
This isn't Javascript, however, the transformation would be pretty straightforward.
void update
{
//Is the button mapped as fire down?
if (!Input.GetButtonDown("Fire1"))
{
Screen.LockCursor = false;
return;
}
Screen.LockCursor = true;
float moveDownY = 0.0f;
// Gets the Y axis movement of the mouse, allows the scroll to be infinite
// If you use mouse position, you'll be limited by screen space
var y = Input.GetAxis("Mouse Y");
moveDownY += y*Sensitivity.sensitivityY;
if (y != 0.0f)
{
// Moves the Camera a distance equivalent to the mouse movement.
Camera.Main.Transform.Translate(Vector3.up * moveDownY);
}
float moveDownX = 0.0f;
//Gets the X axis movement of the mouse
var x = Input.GetAxis("Mouse X");
moveDownX += x * Sensitivity.sensitivityX;
if (x != 0.0f)
{
Camera.Main.Transform.Translate(Vector3.right * moveDownX);
}
}
Answer by Viiarge · Jan 09, 2017 at 03:28 PM
Here is a simple solution that I made :
if (Input.GetMouseButton(1)) {
float sensitivity = 1;
float x = Input.GetAxis("Mouse X");
float y = Input.GetAxis("Mouse Y");
camera.transform.Translate(new Vector3(-x, -y, 0) * sensitivity);
}