Monday, November 4, 2024
spot_img

unity – Adjusting Minimap Digicam Viewport Indicator to Account for Occlusion of Digicam by UI


I’m engaged on a 2D RTS sport in Unity. One factor I need to characterize is a minimap indicator that exhibits the digicam’s viewport. I bought it working, but it surely would not replicate how the digicam is occluded by the highest and backside UI bars. I would like the minimap indicator to solely present what the digicam sees, i.e., all the things not occluded by the highest and backside bars. Here is what it appears to be like like with out the highest and backside bars taken into consideration. The yellow clear sq. is the indicator. Minimap

Here is the code I bought:

public class Minimap : MonoBehaviour, IPointerClickHandler
{
    [SerializeField] Digicam mainCamera;           
    [SerializeField] CameraController mainCameraController;
    [SerializeField] Digicam minimapCamera;         
    [SerializeField] RectTransform minimap;     
    [SerializeField] RectTransform minimapIndicator;
    [SerializeField] RectTransform topBar, bottomBar;
    [SerializeField] Canvas mainCanvas;

    [SerializeField] Vector2 minimapWorldSize;

    void Begin()
    {
        float minimapHeight = minimapCamera.orthographicSize * 2;
        float minimapWidth = minimapHeight * minimapCamera.side;
        minimapWorldSize = new Vector2(minimapWidth, minimapHeight);
    }

    void Replace()
    {
        UpdatePositionAndSize();
    }

    void UpdatePositionAndSize()
    {
        Vector3 mainCameraPos = mainCamera.remodel.place;

        float mainCameraHeight = mainCamera.orthographicSize * 2;
        float mainCameraWidth = mainCameraHeight * mainCamera.side;

        float normalizedX = (mainCameraPos.x - minimapCamera.remodel.place.x + (minimapWorldSize.x / 2)) / minimapWorldSize.x;
        float normalizedY = (mainCameraPos.y - minimapCamera.remodel.place.y + (minimapWorldSize.y / 2)) / minimapWorldSize.y;

        Vector2 newAnchoredPosition = new Vector2(
            (normalizedX * minimap.rect.width) - minimap.rect.width / 2,
            (normalizedY * minimap.rect.top) - minimap.rect.top / 2
        );

        minimapIndicator.anchoredPosition = newAnchoredPosition;

        float indicatorWidth = (mainCameraWidth / minimapWorldSize.x) * minimap.rect.width;
        float indicatorHeight = (mainCameraHeight / minimapWorldSize.y) * minimap.rect.top;

        minimapIndicator.sizeDelta = new Vector2(indicatorWidth, indicatorHeight);
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        RectTransformUtility.ScreenPointToLocalPointInRectangle(minimap, eventData.place, eventData.pressEventCamera, out Vector2 localPoint);

        float normalizedX = (localPoint.x + minimap.rect.width / 2) / minimap.rect.width;
        float normalizedY = (localPoint.y + minimap.rect.top / 2) / minimap.rect.top;

        float targetX = (normalizedX * minimapWorldSize.x) - (minimapWorldSize.x / 2) + minimapCamera.remodel.place.x;
        float targetY = (normalizedY * minimapWorldSize.y) - (minimapWorldSize.y / 2) + minimapCamera.remodel.place.y;

        mainCameraController.OverridePosition(new Vector2(targetX, targetY));
    }
}

I’ve tried calculating the seen digicam top and adjusting the y-position of the indicator, however I all the time get a detrimental top for the indicator. I’m uncertain easy methods to convert the UI models into world models for the seen top calculation after which convert it into the suitable models for the minimap. Here’s what I attempted:

   void UpdatePositionAndSize()
   {
       Vector3 mainCameraPos = mainCamera.remodel.place;

       float mainCameraHeight = mainCamera.orthographicSize * 2;
       float mainCameraWidth = mainCameraHeight * mainCamera.side;

       float topBarHeight = topBar.rect.top;
       float bottomBarHeight = bottomBar.rect.top;
       float visibleCameraHeight = mainCameraHeight - topBarHeight - bottomBarHeight;

       float normalizedX = (mainCameraPos.x - minimapCamera.remodel.place.x + (minimapWorldSize.x / 2)) / minimapWorldSize.x;
       float normalizedY = (mainCameraPos.y - minimapCamera.remodel.place.y + (visibleCameraHeight / 2)) / minimapWorldSize.y;

       Vector2 newAnchoredPosition = new Vector2(
           (normalizedX * minimap.rect.width) - minimap.rect.width / 2,
           (normalizedY * minimap.rect.top) - minimap.rect.top / 2
       );
       minimapIndicator.anchoredPosition = newAnchoredPosition;

       float indicatorWidth = (mainCameraWidth / minimapWorldSize.x) * minimap.rect.width;
       float indicatorHeight = (visibleCameraHeight / minimapWorldSize.y) * minimap.rect.top;
       minimapIndicator.sizeDelta = new Vector2(indicatorWidth, indicatorHeight);
   }

I respect any assist. Thanks!

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -spot_img

Latest Articles