- Home /
Checking a float during lerp
I have an object using Mathf.Lerp and an offset variable for the y position like you see in the headbobber script.
My code is basically this:
if(bool == true){
offset = Mathf.Lerp(offset, desiredValue, speed * Time.deltaTime);
if(offset == desiredValue){
print ("Yeah");
bool = false;
}
}
This works, but my offset variable never actually reaches the desired value no matter the speed. This is really vital for a scripted animation I use for changing equipped items. Is there a way to ensure it reaches that value? No matter what I said the checked value it never prints, my gameobject needs to always end up in the same rest position.
Answer by FLASHDENMARK · Dec 27, 2012 at 01:21 AM
if(offset <= desiredValue + imprecisionFactor && offset >= desiredValue - imprecisionFactor){
//Do your stuff here.
}
offset is a float value and thus you are very unlikely to ever hit that precise number. You will need to check if offset is within a certain value(imprecisionFactor).
Yeah I know it's an old thread but there's a small typo in the second condition. Should be >=
. :)
Your answer
Follow this Question
Related Questions
Mathf.Lerp float between 0 and 1 based on boolean input 3 Answers
Is there a way to smoothly transition between two floats, with it slowing down towards the end 1 Answer
Complicated request for script/help/explanations etc. 1 Answer
How do I make my own Remote Settings backend like Unity Analytics Remote Settings? 0 Answers
Can't change a float 2 Answers