MapCameraController.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using UnityEngine;
  2. using UnityEngine.EventSystems;
  3. public class MapCameraController : MonoBehaviour
  4. {
  5. [Header("Camera Movement")]
  6. public float moveSpeed = 10f;
  7. public float zoomSpeed = 5f;
  8. public float minZoom = 5f;
  9. public float maxZoom = 50f;
  10. public float initialZoom = 35f; // Starting zoom level to see more of the map
  11. private Camera cam;
  12. private Vector3 targetPosition;
  13. private float targetSize;
  14. void Start()
  15. {
  16. cam = GetComponent<Camera>();
  17. if (cam == null)
  18. cam = Camera.main;
  19. // Set camera to look straight down
  20. transform.rotation = Quaternion.Euler(90f, 0f, 0f);
  21. // Position camera above the center of the map (assuming 100x100 map)
  22. transform.position = new Vector3(50f, 30f, 50f);
  23. targetPosition = transform.position;
  24. targetSize = initialZoom;
  25. cam.orthographicSize = initialZoom;
  26. // Ensure camera is orthographic for top-down view
  27. cam.orthographic = true;
  28. }
  29. void Update()
  30. {
  31. // Only handle camera input if mouse is within the map area
  32. if (IsMouseInMapArea())
  33. {
  34. HandleMovement();
  35. HandleZoom();
  36. }
  37. // Always smooth camera movement (even if input is disabled)
  38. transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * 5f);
  39. cam.orthographicSize = Mathf.Lerp(cam.orthographicSize, targetSize, Time.deltaTime * 5f);
  40. }
  41. void HandleMovement()
  42. {
  43. Vector3 moveDirection = Vector3.zero;
  44. // WASD or Arrow Keys movement
  45. if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
  46. moveDirection.z += 1f;
  47. if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
  48. moveDirection.z -= 1f;
  49. if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
  50. moveDirection.x -= 1f;
  51. if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
  52. moveDirection.x += 1f;
  53. // Apply movement
  54. targetPosition += moveDirection.normalized * moveSpeed * Time.deltaTime;
  55. // Keep camera within reasonable bounds (adjust based on your map size)
  56. targetPosition.x = Mathf.Clamp(targetPosition.x, -20f, 120f);
  57. targetPosition.z = Mathf.Clamp(targetPosition.z, -20f, 120f);
  58. }
  59. void HandleZoom()
  60. {
  61. float scroll = Input.GetAxis("Mouse ScrollWheel");
  62. if (scroll != 0f)
  63. {
  64. targetSize -= scroll * zoomSpeed;
  65. targetSize = Mathf.Clamp(targetSize, minZoom, maxZoom);
  66. }
  67. }
  68. /// <summary>
  69. /// Check if the mouse is currently within the map area (not in UI panels)
  70. /// </summary>
  71. /// <returns>True if mouse is in the map area, false if in UI areas</returns>
  72. private bool IsMouseInMapArea()
  73. {
  74. Vector2 mousePosition = Input.mousePosition;
  75. // Define the map area bounds (adjust these values based on your UI layout)
  76. // These values represent the approximate red square area from your image
  77. float leftUIWidth = 150f; // Map Legend panel width
  78. float rightUIWidth = 300f; // Your Team panel width
  79. float topUIHeight = 0f; // No top UI currently
  80. float bottomUIHeight = 0f; // No bottom UI currently
  81. // Calculate the actual map area
  82. float mapLeft = leftUIWidth;
  83. float mapRight = Screen.width - rightUIWidth;
  84. float mapTop = Screen.height - topUIHeight;
  85. float mapBottom = bottomUIHeight;
  86. // Check if mouse is within the map area
  87. bool inMapArea = mousePosition.x >= mapLeft &&
  88. mousePosition.x <= mapRight &&
  89. mousePosition.y >= mapBottom &&
  90. mousePosition.y <= mapTop;
  91. return inMapArea;
  92. }
  93. }