Description:
I'm having trouble with a camera dragging script in Unity. The camera is supposed to move when the mouse is dragged, but it's not working as expected. Here's a detailed description of what I've done and the issue I'm facing:
Steps Taken:
Created a New Project:
Created a new 2D project in Unity.
Added a Canvas to the scene.
Added two UI elements (Panel) as children of the Canvas.
Created a CameraController Script:
Created a new script called CameraController.cs with the following code:
using UnityEngine;
public class CameraController : MonoBehaviour
{
public float panSpeed = 20f;
private bool isDragging = false;
private Vector3 dragOrigin;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
isDragging = true;
dragOrigin = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Debug.Log("Mouse button down");
return;
}
if (!Input.GetMouseButton(0))
{
isDragging = false;
Debug.Log("Mouse button up");
}
if (isDragging)
{
Vector3 dragPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 move = dragOrigin - dragPos;
transform.position += move;
Debug.Log("Dragging camera");
}
}
}
Attached the Script to the Camera:
Attached the CameraController script to the Main Camera.
Checked Input Settings:
Ensured that the input settings in Unity are correctly configured (Edit -> Project Settings -> Input Manager).
Checked Canvas Settings:
Ensured that the Canvas is set to Screen Space - Camera and is linked to the Main Camera.
issue:
When I run the scene, the console continuously logs "Mouse button up" even when I'm not clicking the mouse.
The camera does not move when I drag the mouse.