How to make a progress bar increase as it gets closer to an object?

the trouble is that you would need to find the closest point of the boundary then calculate the distance to that point on a very regular basis which could cause a performance loss. depending on your situation you may want to look into just using something like a collision volume to tell the player they are going out of bounds, a hollow blocking volume, or just calculate the distance from the center of the level. the last option mentioned is very easy, you just get max distance from center and player location then normalize to range which will return a 0-1 value.

if you choose to pursue your current method though below is a example of the logic needed. basically you need to have an array of points which represent the boundary of the level, then you need to find if there was a line between each point where on the line am i closest to. thats basically the entire top half of the script, for line from point 0-1 how far is the player from the line, is that closer than the current closest line value, if yes set this line to the closest, repeat for line points 1-2, etc.

now this would all work well for a circle where all points are equal from the center, but in the case of square or irregular shape we need to provide a max range value so we have something to normalize to since a progress bar requires a 0-1 value.