CameraController.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using UnityEngine;
  2. using UnityEngine.EventSystems;
  3. public class CameraController : MonoBehaviour
  4. {
  5. public float moveSpeed = 10f;
  6. public float rotationSpeed = 100f;
  7. public float zoomSpeed = 10f;
  8. public float minZoom = 5f;
  9. public float maxZoom = 50f;
  10. private Transform cam;
  11. void Start()
  12. {
  13. cam = Camera.main.transform;
  14. }
  15. void Update()
  16. {
  17. // Only handle camera input if mouse is within the map area
  18. if (IsMouseInMapArea())
  19. {
  20. float h = Input.GetAxis("Horizontal");
  21. float v = Input.GetAxis("Vertical");
  22. transform.Translate(new Vector3(h, 0, v) * moveSpeed * Time.unscaledDeltaTime, Space.World);
  23. float moveX = 0, moveZ = 0;
  24. if (Input.GetKey(KeyCode.W)) moveZ += 1;
  25. if (Input.GetKey(KeyCode.S)) moveZ -= 1;
  26. if (Input.GetKey(KeyCode.A)) moveX -= 1;
  27. if (Input.GetKey(KeyCode.D)) moveX += 1;
  28. transform.Translate(new Vector3(moveX, 0, moveZ) * moveSpeed * Time.unscaledDeltaTime, Space.World);
  29. if (Input.GetKey(KeyCode.Q))
  30. {
  31. transform.Rotate(Vector3.up, -rotationSpeed * Time.unscaledDeltaTime);
  32. }
  33. if (Input.GetKey(KeyCode.E))
  34. {
  35. transform.Rotate(Vector3.up, rotationSpeed * Time.unscaledDeltaTime);
  36. }
  37. float scroll = Input.GetAxis("Mouse ScrollWheel");
  38. if (scroll != 0f)
  39. {
  40. Vector3 pos = cam.localPosition;
  41. pos += cam.forward * scroll * zoomSpeed;
  42. pos = Vector3.ClampMagnitude(pos, maxZoom);
  43. pos.y = Mathf.Clamp(pos.y, minZoom, maxZoom);
  44. cam.localPosition = pos;
  45. }
  46. }
  47. }
  48. /// <summary>
  49. /// Check if the mouse is currently within the map area (not in UI panels)
  50. /// </summary>
  51. /// <returns>True if mouse is in the map area, false if in UI areas</returns>
  52. private bool IsMouseInMapArea()
  53. {
  54. Vector2 mousePosition = Input.mousePosition;
  55. // Define the map area bounds (adjust these values based on your UI layout)
  56. // These values represent the approximate red square area from your image
  57. float leftUIWidth = 150f; // Map Legend panel width
  58. float rightUIWidth = 300f; // Your Team panel width
  59. float topUIHeight = 0f; // No top UI currently
  60. float bottomUIHeight = 0f; // No bottom UI currently
  61. // Calculate the actual map area
  62. float mapLeft = leftUIWidth;
  63. float mapRight = Screen.width - rightUIWidth;
  64. float mapTop = Screen.height - topUIHeight;
  65. float mapBottom = bottomUIHeight;
  66. // Check if mouse is within the map area
  67. bool inMapArea = mousePosition.x >= mapLeft &&
  68. mousePosition.x <= mapRight &&
  69. mousePosition.y >= mapBottom &&
  70. mousePosition.y <= mapTop;
  71. return inMapArea;
  72. }
  73. }