#Camera Dragging Issue in Unity

1 messages · Page 1 of 1 (latest)

tame dagger
#

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.

#

Additional Information:
I've tried creating a new project and following the same steps, but the issue persists.
I've checked for other scripts or objects that might be interfering with the mouse input, but there are none.
I've ensured that the EventSystem is present in the scene.