- 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: $$anonymous$$ = 1 max = 3 control = 2
So the $$anonymous$$athf.Clamp() keeps control between $$anonymous$$ and max, but right now that doesn't matter, so now we have: = (2 - $$anonymous$$) / (max - $$anonymous$$)
So now if we plug in the $$anonymous$$ 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 <= $$anonymous$$)
{
//debug.log("0");
}
Thanks
$$anonymous$$athf.Clamp doesn't get exactly I need. $$anonymous$$athf.Clamp clamps the number to be $$anonymous$$ and max. It doesn't allow the output to be 0 at $$anonymous$$ and 1 at max.
I guess you could also try $$anonymous$$athf.$$anonymous$$ax($$anonymous$$, $$anonymous$$athf.$$anonymous$$in(max, control))
Answer by realbambiiyt · Jan 06, 2021 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

Follow this Question
Related Questions
Get the point between 2 other points using weights 1 Answer
Find connections dont working 0 Answers
Cave Algorithm Help 0 Answers
Algorithm used for pathfinding using NavMesh by unity? 1 Answer
Room Path Generator 1 Answer