- Home /
Click and Drag Camera
So I've created a simple camera script to click and drag the game view around. Using the left mouse button to click inside the game it "grabs" the ground and then calculates the difference between where you clicked and where you moved the mouse to, then moves the cameras position accordingly to simulate it all. The problem i'm having is that because i'm using ScreenToViewportPoint to calcuate this (I couldn't get ScreenToWorldPoint to work) every time I click it moves the camera back to 0,0,0. So everything works great as long as I don't need to click to drag more than one time... I've tried tons of things and can't get anything to work. Here's my code:
void MousePan()
{
if(Input.GetMouseButtonDown(0))
{
panOrigin = Camera.main.ScreenToViewportPoint(Input.mousePosition); //Get the ScreenVector the mouse clicked
}
if(Input.GetMouseButton(0))
{
Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition) - panOrigin; //Get the difference between where the mouse clicked and where it moved
transform.position = -pos * panSpeed; //Move the position of the camera to simulate a drag, speed * 10 for screen to worldspace conversion
}
}
Is there a reason you're not doing something like this ins$$anonymous$$d?
float h = horizontalSpeed * Input.GetAxis("$$anonymous$$ouse X");
float v = verticalSpeed * Input.GetAxis("$$anonymous$$ouse Y");
Camera.main.transform.Translate(h, 0, v);
With that method the mouse movement doesn't translate well and you end up moving way further in the game than you actually move your mouse. The other reason is with that method, as long as your holding the mouse button the camera will keep moving.
$$anonymous$$y code was meant to make it feel like you're literally grabbing the ground and having a 1:1 movement effect on the camera.
Thanks for the response though!
Answer by Grimshad · Nov 10, 2014 at 12:34 AM
Figured it out:
if(Input.GetMouseButtonDown(0))
{
bDragging = true;
oldPos = transform.position;
panOrigin = Camera.main.ScreenToViewportPoint(Input.mousePosition); //Get the ScreenVector the mouse clicked
}
if(Input.GetMouseButton(0))
{
Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition) - panOrigin; //Get the difference between where the mouse clicked and where it moved
transform.position = oldPos + -pos * panSpeed; //Move the position of the camera to simulate a drag, speed * 10 for screen to worldspace conversion
}
if(Input.GetMouseButtonUp(0))
{
bDragging = false;
}
Thank you very much for posting the solution! It helped a lot =]
Answer by crare · Nov 15, 2020 at 10:17 AM
Panning with using Mouse axes from input manager using Input.GetAxis().
You can inverse the pan direction by removing the minus character from transfrom.Translate()-method.
Also you may want to use z axis instead of y depending on your configuration.
public class CameraScript : MonoBehaviour
{
public float panSpeed = 10f;
void Update()
{
if (Input.GetMouseButton(1)) // right mouse button
{
var newPosition = new Vector3();
newPosition.x = Input.GetAxis("Mouse X") * panSpeed * Time.deltaTime;
newPosition.y = Input.GetAxis("Mouse Y") * panSpeed * Time.deltaTime;
// translates to the opposite direction of mouse position.
transform.Translate(-newPosition);
}
}
}
Your answer
Follow this Question
Related Questions
Bullet mouse control 2 Answers
How to check for a mouse drag 0 Answers
OnMouseUp() Click Display Effect. 1 Answer
How to disable button OnClick when drag on the screen with OnMouseDrag 2 Answers
How to properly set the position clicked by the mouse? 0 Answers