Positioning players on a UI map (relative to frame size)

I’m trying to add players to a radar minimap, however I’m struggling to get their characters accurate on the map, in terms of their real position on the map

This problem is very simple to fix if the frame is square (AspectRatio 1:1) and it’s just a matter of finding a scale to use when dividing the distance (I found 45 to be most accurate)

local Offset = character.PrimaryPart.Position - HumanoidRootPart.Position

local Distance = 1 / 45
				Offset *= Distance
				PreviousCharacter.Position = UDim2.new(
					Offset.Z + 0.5,
					0,
					-Offset.X + 0.5,
					0
				)

ezgif.com-gif-maker (66)

However, this doesn’t work as evenly when the frame isn’t even (rectangular shape)
ezgif.com-gif-maker (67)
As you can see, when I move left - right, it looks fine (the paw stays on the path in the map on Y value of the UDim2.)
However moving up and down is where it goes off

I thought of using the Frame.AbsoluteSize to get valid scales, and that way it could work no matter what sizes I use. However I can’t get anywhere close.

2 Likes

Is the minimap a ViewportFrame or an ImageLabel?

Viewport. Character icons are imagelabels tho

Wow I spent too long on this for such a simple answer. First a bit of information about how I set up a little demo so I could test things until one of my ideas worked.

image

The size is determined by Container. Both the children of container are set to size {1,0,1,0}

I tried doing this mathematically in the code for a while before I gave up, then I just made this slight adjustment to the Overlay frame which holds the image label tracker.

image
And that fixed it. Though keep in mind I also set the position to {0.5,0,0.5,0} and I set the anchor point to 0.5,0.5 to translate it over because it would become offset with the default topleft alignment.

Also just a side note on something I did differently. Instead of approximating the scale like you appear to have done (1/45) I used camera:WorldToViewportPoint()

local targetPos = cam:WorldToViewportPoint(target.Position)
targetLabel.Position = UDim2.fromScale(targetPos.X, targetPos.Y)

I still feel like there is a very simple math way to do it that I simply didn’t find that would essentially be doing the same thing as changing the size constraint, but this works.

1 Like