How do I convert distance to weighting?

I’m working on a targeting system that evaluates every nearby enemy and determines who is the best target. I can get linear distance really easily with this:

float maximumRange = 15f; //maximum distance an enemy may be hit from
float distance = Vector3.Distance (attacker.transform.position, target.transform.position);

However, ideally what I need is to convert each distance to a value between 0-1, where an enemy standing on top of player (and thus matching his coordinates) receives a 1, and an enemy whose distance==maximumRange recieves a 0.1. Is there a simple way to compute this, or am I looking at a whole lot of legwork?

I would have done simply as follow :

float maximumRange = 15f; //maximum distance an enemy may be hit from
float distance = Vector3.Distance (attacker.transform.position, target.transform.position);
float weight = 1 - Mathf.Clamp( distance, 0, maximumRange) / maximumRange ;

If the distance is 0 : distance / maximumRange = 0 => weight = 1

If the distance is maximumRange : distance / maximumRange = 1 => weight = 0

EDIT : Adding Mathf.Clamp to keep the weight positive. Good remark @fafase ! :wink: