UnityGameProjectsCode / DiceAndMenGame / CameraControl.cs
CameraControl.cs
Raw
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraControl : MonoBehaviour
{
    [SerializeField] Vector3 range, speed;

    [SerializeField] Transform cameraAnchor, cameraTarget;

    [SerializeField] string horizontalAxis, verticalAxis, zoomAxis;

    private void Start()
    {
        cameraTarget.SetParent(cameraAnchor);
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 delta = new Vector3(
                Input.GetAxis(horizontalAxis),
                Input.GetAxis(verticalAxis),
                Input.GetAxis(zoomAxis));

        delta.Scale(speed);

        var pos = cameraTarget.localPosition + delta * Time.deltaTime;

        cameraTarget.localPosition = new Vector3(
            Mathf.Clamp(pos.x, -range.x / 2, range.x / 2),
            Mathf.Clamp(pos.y, -range.y / 2, range.y / 2),
            0);
    }

    private void OnDrawGizmos()
    {
        if (!cameraAnchor)
            return;

        Gizmos.color = new Color(1, 0, 1, .1f);

        Gizmos.DrawCube(cameraAnchor.position, range);
    }
}