Drag 3d camera

Hello, I am working on a tycoon game and using an isometric camera with a fixed angle. I would like to allow players to move the camera by clicking with the mouse (or fingers on mobile devices) on the screen. However, the camera should only move along the x and z axes. Can anyone suggest a method or provide code examples on how to achieve this?

You can create a script to attach to your camera that will handle the mouse (or finger) dragging for moving the camera along the X and Z axes. Here’s a simple example for moving the camera with the mouse which should be enough to get you started down the right path (touch input can be added later):

using UnityEngine;

public class CameraDragController : MonoBehaviour
{
    public float dragSpeed = 1f;
    private Vector3 dragOrigin;

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            dragOrigin = Input.mousePosition;
            return;
        }

        if (!Input.GetMouseButton(0)) return;

        Vector3 pos = Camera.main.ScreenToViewportPoint(dragOrigin - Input.mousePosition);
        Vector3 move = new Vector3(pos.x * dragSpeed, 0, pos.y * dragSpeed);

        transform.Translate(move, Space.World);
    }
}

Attach this script to your isometric camera. You can adjust the dragSpeed variable to control how fast the camera moves when dragged.

To add touch input support for mobile devices, you can use Unity’s Input.touches or Input.GetTouch functions to handle touch input. For more information on handling touch input, you can refer to the Unity documentation on touches.