- Home /
Can I stop a lerp completely when it gets to 20% of its original value?
I use the code bellow to move images across the screen as seen in this video: http://screencast.com/t/GW9K8bbQcmYj
Is there a way to stop the lerp completely when it gets to say 20% of it's intial value just to make the final stage of the sliding slow down quicker - as I intend to make it 'bounce' back to the last full image so that the user is never left with half an image on the screen.
I do like the slide effect when the user stops swiping but after a while it still moves a little even though nothing is visible to the eye it's that small movmement I'm trying to get rid of.
code wrong.
Answer by loopyllama · Mar 09, 2011 at 04:17 PM
you just need to rewrite your code a little bit.
right now you are lerping a constant amount every update. It is a lot like saying "I want to reach the wall so I will go 50% of the way to the wall every step"...you never quite get there. how is this happening? lerp's 3 argument determines the blend between A and B, with 0 being 100% A, and 1 being 100% B. You are setting the value to four times the delta between frames. I dunno what that is but it is small. So on frame 1 you go some small (practically constant) amount from where you are to your target, then repeat. What you want to do is move from where your swipe STARTED to your target over an ELAPSED time. something like: capture the startposX in a variable when you detect a swipe and the startTime = Time.time of the swipe. Also maybe a boolean hasSwiped. Then in do a if (hasSwiped) so you are not processing if all swipes are complete. in this if(hasSwiped), calc the elapsedTime = Time.time - startTime then do your lerp. something like lerp (startPosX, targetPosX, myScrollingInSeconds / elapsedTime) where myScrollingInSeconds in the time in seconds to lerp from the start pos to the target pos. you will find linear works as advertised and you will probably desire something like SmoothStep. it is tough for me to explain so I hope it made sense.
Answer by Bampf · Mar 09, 2011 at 12:04 PM
A simple way is to use a non-linear version of Lerp. There are a bunch of them in the Unify wiki in the Mathfx script. You'd want one that goes faster during the earlier stage, then spends less time slowing down at the end of the movement. (This is known as an "ease-out".) Try using their Mathfx.Sinerp function in place of your Lerp.
You may also need to adjust the end position calculation as well. Right now it's based solely on speed and time delta. If you multiple it by 0.8 (for example) your object will move faster to start with and then slow down at the end, but only have to go 80% of the way. You may have to play with that and/or the speed constant until it feels like you want right.
Answer by Statement · Mar 09, 2011 at 01:45 PM
You can add an AnimationCurve that you evaluate instead of the Lerp, or plug it into the lerp.
It's editable in the inspector and allow you to set the values easily. Just add it as a member variable.
var curve : AnimationCurve;
Then use it as such:
var value : float = curve.Evaluate(point);
Just make sure your curve spans the points expected, for example the range 0 to 1.