Minimap Math Question

I almost got my Metroid like minimap working, this is the large map when I press pause and look at the map. But I am having some issues converting the player position to the UI position…
Here is some information:

1. My minimap image size is 720x720 and
I have the rooms set up to be square
2. A room in the image is 16x16 pixels
(with a small border around it).
3. My BlipMarker needs to move 16 in
the UI canvas to be centered in each
room (this depends on the scale of
the minimap, right now it’s at
100%).
4. I have my code to have different
width and height since my camera is
a rectangle (widescreen) and I need
the player to snap to a square.

This is my code:

``````public Transform playerLocation;
public RectTransform BlipMarker;

void LateUpdate()
{
// Get the RectTransform component
RectTransform BlipMarker = GetComponent<RectTransform>();

// Place the blip at the correct location on the minimap image
BlipMarker.anchoredPosition = RoundPosition(playerLocation.position);
}
public Vector3 RoundPosition(Vector3 position)
{
float segmentWidth = 25f;   // Camera is 25 units wide
float segmentHeight = 15f;  // Camera is 15 units high
position.x = Mathf.FloorToInt(position.x / segmentWidth) * segmentWidth;
position.y = Mathf.FloorToInt(position.y / segmentHeight) * segmentHeight;
return position;
}
``````

Now, obviously my BlipMarker image snaps each 25 sideways and 15 upwards etc in the UI, now I need to find a way to make it snap to 16’s not 25’s and 15’s, respectively, is there a way to do this?

So 25 would need to be 16 and 50 would need to be 32 in the blip movement etc. but I need to have my segmentWidth and segmentHeight to those sizes because of the camera size. (unless there’s a better way of doing this of course)

I may not have understood this correctly, so apologies if that’s the case.

camera width (25) / segment (16) = ratio (1.56)

So now you do all your normal calculations, dividing the position by your ratio (1.56).

This way, even if you had a 1:1 ratio (ratio=1), you can leave all the calculations in place and it’ll still work.

`````` public Transform playerLocation;
public RectTransform BlipMarker;
void LateUpdate()
{
// Get the RectTransform component
RectTransform BlipMarker = GetComponent<RectTransform>();
// Place the blip at the correct location on the minimap image
BlipMarker.anchoredPosition = RoundPosition(playerLocation.position);
}
public Vector3 RoundPosition(Vector3 position)
{
float segmentWidth = 25f;   // Camera is 25 units wide
float segmentHeight = 15f;  // Camera is 15 units high

float roomTile=16f;

float widthRatio=segmentWidth / roomTile;
float heightRatio=segmentHeight / roomTile;

position.x = Mathf.FloorToInt(position.x / (roomTile * widthRatio));
position.y = Mathf.FloorToInt(position.y / (roomTile * heightRatio));
return position;
}
``````

Thanks you!!!
I just added “return position * roomTile;” to get the correct snap for the BlipMarker.