using UnityEngine; using UnityEngine.EventSystems; public class MapCameraController : MonoBehaviour { [Header("Camera Movement")] public float moveSpeed = 10f; public float zoomSpeed = 5f; public float minZoom = 5f; public float maxZoom = 50f; public float initialZoom = 35f; // Starting zoom level to see more of the map private Camera cam; private Vector3 targetPosition; private float targetSize; void Start() { cam = GetComponent(); if (cam == null) cam = Camera.main; // Set camera to look straight down transform.rotation = Quaternion.Euler(90f, 0f, 0f); // Position camera above the center of the map (assuming 100x100 map) transform.position = new Vector3(50f, 30f, 50f); targetPosition = transform.position; targetSize = initialZoom; cam.orthographicSize = initialZoom; // Ensure camera is orthographic for top-down view cam.orthographic = true; } void Update() { // Only handle camera input if mouse is within the map area if (IsMouseInMapArea()) { HandleMovement(); HandleZoom(); } // Always smooth camera movement (even if input is disabled) transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * 5f); cam.orthographicSize = Mathf.Lerp(cam.orthographicSize, targetSize, Time.deltaTime * 5f); } void HandleMovement() { Vector3 moveDirection = Vector3.zero; // WASD or Arrow Keys movement if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)) moveDirection.z += 1f; if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow)) moveDirection.z -= 1f; if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) moveDirection.x -= 1f; if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) moveDirection.x += 1f; // Apply movement targetPosition += moveDirection.normalized * moveSpeed * Time.deltaTime; // Keep camera within reasonable bounds (adjust based on your map size) targetPosition.x = Mathf.Clamp(targetPosition.x, -20f, 120f); targetPosition.z = Mathf.Clamp(targetPosition.z, -20f, 120f); } void HandleZoom() { float scroll = Input.GetAxis("Mouse ScrollWheel"); if (scroll != 0f) { targetSize -= scroll * zoomSpeed; targetSize = Mathf.Clamp(targetSize, minZoom, maxZoom); } } /// /// Check if the mouse is currently within the map area (not in UI panels) /// /// True if mouse is in the map area, false if in UI areas private bool IsMouseInMapArea() { Vector2 mousePosition = Input.mousePosition; // Define the map area bounds (adjust these values based on your UI layout) // These values represent the approximate red square area from your image float leftUIWidth = 150f; // Map Legend panel width float rightUIWidth = 300f; // Your Team panel width float topUIHeight = 0f; // No top UI currently float bottomUIHeight = 0f; // No bottom UI currently // Calculate the actual map area float mapLeft = leftUIWidth; float mapRight = Screen.width - rightUIWidth; float mapTop = Screen.height - topUIHeight; float mapBottom = bottomUIHeight; // Check if mouse is within the map area bool inMapArea = mousePosition.x >= mapLeft && mousePosition.x <= mapRight && mousePosition.y >= mapBottom && mousePosition.y <= mapTop; return inMapArea; } }