This site uses strictly necessary cookies. More Information

X- Home /

# Algorithm Help

Hello. I am having troubles creating a algorithm that fits my needs.

So I have a minimum value, a maximum value, and a control value.

The control value can go below the minimum value and above the maximum value.

I need it so the output value is 0.0 when Control <= Minimum, and 1.0f when Control >= Maximum.

**Answer** by zardini123
·
Jul 22, 2015 at 03:54 PM

Okay I figured it out. The Mathf.Clamp helps a little.

```
(Mathf.Clamp(control, min, max) - min) / (max - min);
```

I was thinking along the lines of an Inverse Lerp. Where you would put in your float and it would throw out t between 0 and 1.

So to help you understand my algorithm:

Say: min = 1 max = 3 control = 2

So the $$anonymous$$athf.Clamp() keeps control between min and max, but right now that doesn't matter, so now we have: = (2 - min) / (max - min)

So now if we plug in the min and max: = (2 - 1) / (3 - 1) = (1) / (2) = 0.5

So there is a $$anonymous$$athf.InverseLerp already! :)

**Answer** by kidmosey
·
Jul 22, 2015 at 05:42 AM

Mathf.Clamp(control, min, max)

Alternatively, if you need to include min/max, you have (similar to the suggested if/else block):

```
Mathf.Max(min, Mathf.Min(max, control))
```

Hi,

If I have correctly understood your problem,

```
if(control > max)
{
//Debug.log("1")
}
else if(control <= min)
{
//debug.log("0");
}
```

Thanks

$$anonymous$$athf.Clamp doesn't get exactly I need. $$anonymous$$athf.Clamp clamps the number to be min and max. It doesn't allow the output to be 0 at min and 1 at max.

I guess you could also try $$anonymous$$athf.$$anonymous$$ax(min, $$anonymous$$athf.$$anonymous$$in(max, control))

**Answer** by realbambiiyt
·
Jan 06 at 11:53 PM

Hello! I know this is a very old thread, but I have come across it because I have a similar issue to the one OP has. For context, I am using the distance between two position vectors for the control variable, a min of 1, and a max of 20. The goal is that the closer I get to my target position, the output value of the algorithm increases from 0 to 1. However, with the solution OP has suggested I do the exact opposite, decreasing from 1 to 0 the closer I get. Any help would be really appreciated, but I'll continue to toy with this for a little while too.

This is not an answer to the question. Please ask a seperate question. This is a Q&A site and not a forum.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Get the point between 2 other points using weights 1 Answer

My precedural Algorithm for Tidy TileMapper is not working! 3 Answers

How to create seemingly random arrangement of tiles in a grid? 1 Answer

2D Geometry / Topology Algorithm help 2 Answers

Detecting need of generating terrain (Minecraft-like) [Perlin Noise] 2 Answers