- Home /
Question by
pranshusharma · Apr 22, 2020 at 06:37 AM ·
mousemouse positionmouse-dragclimbing
Moving camera on mouse drag by a fixed distance each time
Hi, Currently I am working on moving a fixed distance with each mouse drag. For example, pressing and dragging the mouse will only move my camera at a max of say 5 units. The camera should move inversely to the direction of the mouse drag. This is down for the implementation of climbing. But even on dragging, the camera keeps on moving till I stop the button. My basic idea is to make a climbing functionality using mouse drags. I cannot think of anything to restrict the distance at each click. Please help.
Here is the sample code:
// VARIABLES
public float panSpeed = .001f;
private Vector3 mouseOrigin;
private bool isPanning;
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(1))
{
mouseOrigin = Input.mousePosition;
//right click was pressed
isPanning = true;
}
// cancel on button release
if (!Input.GetMouseButton(1))
{
isPanning = false;
}
//move camera on X & Y
if (isPanning)
{
Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin);
// update x and y but not z
Vector3 move = new Vector3(pos.x, pos.y, 0) * panSpeed;
Camera.main.transform.Translate(-move, Space.Self);
}
}
Comment