| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- using UnityEngine;
- using UnityEngine.EventSystems;
- public class CameraController : MonoBehaviour
- {
- public float moveSpeed = 10f;
- public float rotationSpeed = 100f;
- public float zoomSpeed = 10f;
- public float minZoom = 5f;
- public float maxZoom = 50f;
- private Transform cam;
- void Start()
- {
- cam = Camera.main.transform;
- }
- void Update()
- {
- // Only handle camera input if mouse is within the map area
- if (IsMouseInMapArea())
- {
- float h = Input.GetAxis("Horizontal");
- float v = Input.GetAxis("Vertical");
- transform.Translate(new Vector3(h, 0, v) * moveSpeed * Time.unscaledDeltaTime, Space.World);
- float moveX = 0, moveZ = 0;
- if (Input.GetKey(KeyCode.W)) moveZ += 1;
- if (Input.GetKey(KeyCode.S)) moveZ -= 1;
- if (Input.GetKey(KeyCode.A)) moveX -= 1;
- if (Input.GetKey(KeyCode.D)) moveX += 1;
- transform.Translate(new Vector3(moveX, 0, moveZ) * moveSpeed * Time.unscaledDeltaTime, Space.World);
- if (Input.GetKey(KeyCode.Q))
- {
- transform.Rotate(Vector3.up, -rotationSpeed * Time.unscaledDeltaTime);
- }
- if (Input.GetKey(KeyCode.E))
- {
- transform.Rotate(Vector3.up, rotationSpeed * Time.unscaledDeltaTime);
- }
- float scroll = Input.GetAxis("Mouse ScrollWheel");
- if (scroll != 0f)
- {
- Vector3 pos = cam.localPosition;
- pos += cam.forward * scroll * zoomSpeed;
- pos = Vector3.ClampMagnitude(pos, maxZoom);
- pos.y = Mathf.Clamp(pos.y, minZoom, maxZoom);
- cam.localPosition = pos;
- }
- }
- }
- /// <summary>
- /// Check if the mouse is currently within the map area (not in UI panels)
- /// </summary>
- /// <returns>True if mouse is in the map area, false if in UI areas</returns>
- 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;
- }
- }
|