Created
November 30, 2025 08:49
-
-
Save Mike-Schvedov/638b0a360c816761dd0bab3d837ec363 to your computer and use it in GitHub Desktop.
MinimapClickHandler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| using UnityEngine.EventSystems; | |
| public class MinimapClickHandler : MonoBehaviour, IPointerClickHandler | |
| { | |
| public Camera minimapCamera; | |
| [Header("Fine-tune offsets")] | |
| public float offsetX = -12f; // Use your own values | |
| public float offsetZ = -15f; // Use your own values | |
| public void OnPointerClick(PointerEventData eventData) | |
| { | |
| RectTransform rt = GetComponent<RectTransform>(); | |
| Vector2 localPoint; | |
| if (!RectTransformUtility.ScreenPointToLocalPointInRectangle( | |
| rt, eventData.position, eventData.pressEventCamera, out localPoint)) | |
| return; | |
| Vector2 size = rt.rect.size; | |
| Vector2 normalized = (localPoint + size * 0.5f) / size; | |
| // Minimap UV → world point | |
| Vector3 worldPoint = minimapCamera.ViewportToWorldPoint( | |
| new Vector3(normalized.x, normalized.y, minimapCamera.transform.position.y) | |
| ); | |
| worldPoint.y = 0f; // ground | |
| // Stop following | |
| if (RTSCameraController.instance.followTransform != null) | |
| RTSCameraController.instance.followTransform = null; | |
| // Get the camera child offset | |
| Vector3 localOffset = RTSCameraController.instance.cameraTransform.localPosition; | |
| // Rotate the offset by the controller's rotation | |
| Vector3 rotatedOffset = RTSCameraController.instance.transform.rotation * localOffset; | |
| // Move parent so camera child lands at worldPoint | |
| Vector3 correctParentPos = worldPoint - rotatedOffset; | |
| // Fine-tune offsets | |
| correctParentPos.x += offsetX; | |
| correctParentPos.z += offsetZ; | |
| // Move your camera controller using your existing logic | |
| RTSCameraController.instance.SetPositionFromMinimap(correctParentPos); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment