Making camera move with right click and drag
Hello
I am new to unity and game programming and am trying to get a basic script working from googling and searching the documentation.
I have wrote a script which i applied to main camera as a component, there are no syntax errors but when i run the game i do not get the expected behavior and am out of guesses on what i did wrong.
Simply i am trying to move the camera on the X & Y axis with a right click and drag motion of the mouse with a script but it did not work.
This is my script attempt, perhaps some one can see my mistake:
using UnityEngine;
using System.Collections;
public class cameraMove : MonoBehaviour {
// VARIABLES
public float panSpeed = 4.0f;
private Vector3 mouseOrigin;
private bool isPanning;
// Update is called once per frame
void Update ()
{
mouseOrigin = Input.mousePosition;
if (Input.GetMouseButtonDown (1))
{
//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 * panSpeed, pos.y * panSpeed, 0);
Camera.main.transform.Translate (move, Space.Self);
}
}
}
Answer by Doodums · Mar 08, 2016 at 11:56 AM
It seems you are setting your mouseOrigin variable every Update() instead of setting it once when the mouse is first clicked. That would make the line:
Vector3 pos = Camera.main.ScreenToViewportPoint (Input.mousePosition - mouseOrigin);
Always return a vector of (0,0,0), as you are basically just saying:
Vector3 pos = Camera.main.ScreenToViewportPoint (Input.mousePosition - Input.mousePosition);
Because you are setting this variable every Update():
mouseOrigin = Input.mousePosition;
Try changing your first if to this:
if (Input.GetMouseButtonDown (1))
{
//right click was pressed
mouseOrigin = Input.mousePosition;
isPanning = true;
}
If that doesn't help you, there are some examples here that might help you: http://forum.unity3d.com/threads/click-drag-camera-movement.39513/
Ah thank you for explaining i did notice it was basically giving me 0,0,0 and i could not work out why!
Answer by Naoks1 · May 10, 2017 at 11:53 PM
@Doodums I know that this is an old post, but could you explain what this code does? I'm new to programming, and just want some information on mouse position. Thanks!
Pretty late but here's step by step breakdown:
When the mouse button is pushed down they store the mouse position (Input.Get$$anonymous$$ouseButtonDown(1)
When the mouse is not being pressed they change the isPanning value to false, stopping the transform from updating in response to changes in the mouse's position.
While the mouse is being held (Input.Get$$anonymous$$ouseButton(1)) they:
a) take the difference between the current mouse position and the mouse position they saved when the mouse was first pressed.
b) Convert the difference from screen space (2D space using pixels) to viewport space (2D space with numbers between 0 and 1)
c) Scale that difference by a factor (panSpeed) -- you may also want to normalize your offset by multiplying by time.deltaTime here
d) $$anonymous$$ake a 3D based on the 2D offset they have calculated by using the x and y coordinates of the 2D vector
e) Add the 3D offset to the camera's transform moving the camera
Input.mousePosition : The current position of the mouse in pixels
Input.Get$$anonymous$$ouseButtonDown(1)) : Returns true the frame the mouse button is first pressed on
Input.Get$$anonymous$$ouseButton(1): Returns true if the mouse button is being pressed (every frame after press)
The mouse functions take an integer that deter$$anonymous$$es which mouse button is checked (0: Left-$$anonymous$$ouse, 1: Right-$$anonymous$$ouse, 2: $$anonymous$$iddle-$$anonymous$$ouse)
If you want to know what a line of code is doing try looking it up in the unity scripting reference online.
Answer by mowax74 · Nov 13, 2020 at 02:07 PM
But with that solution the dragging speed is set by a multiplicator, so the mouse pointer stays not on top of the point in 3d space to the time it was clicked.
Wondering if there is a solution where after every frame of camera dragging the same spot in 3d space is below the mouse pointer. That behaviour would feel way more comfortable. With the above solution the cam movement tends to be to slow or to fast, it's not possible to adjust it to the exact movement of the mouse.
Your answer
![](https://koobas.hobune.stream/wayback/20220612071015im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Editor is lagging 0 Answers
2D How to set a limit to the rotation of my cube, when dragging with mouse 0 Answers
How to add camera and movement borders 1 Answer
camera drag 0 Answers
Multiple camera and camera relative player movement 0 Answers