Lerping in Unity is easy. There is no tool I use more in game development than lerping. In this tutorial I'll show you what it is, how it works and why you should use it.
Move, rotate and scale your objects using lerp while making it sexier with animation curves.
Lerping, or 'linear interpolation' is a formula to find a point between two numbers given a percentage value.
Your Unity skills are about to transform!
❤️ Become a Tarobro on Patreon: https://www.patreon.com/tarodev
=========
🔔 SUBSCRIBE: https://bit.ly/3eqG1Z6
🗨️ DISCORD: https://discord.gg/GqeHHnhHpz
✅ MORE TUTORIALS: https://www.youtube.com/tarodev

in this video i'll teach you what lerp is how it works and why it should be a

vital tool in your game dev toolkit let's get into it

okay so to get started let's create a script and call it leper or whatever

you'd like to call it it will function the same

okay so before we actually start learning stuff let's learn what lerp

does and i'm just going to do that in javascript because it'll be super easy

for me to show you so given two points let's say a is equal to zero and b is

equal to ten we would like to find a percentage value

between these two numbers so let's say we want to find forty percent of the way

to ten so we can do that with our t for time

and let's just make this 0.4 so we want to find the 40 between 0 and

10. now

uh you don't need to know the maths about about how lerp works you kind of

just need to know like what it does because unity makes it very easy but i'm

just going to glance over the maths right now so let's say our point the

point the 40 point is going to be and this is the formula for lerp

uh it's one minus t so if if t maximum is 1 and we've given

it 0.4 the remainder here will be 0.6 times ra

plus time

multiplied by b now if we console log out our point here

whoops that's not what i wanted you'll see that it is four so that is

forty percent of the way between zero and ten and if we change this to say 3

or 0.33 uh it will

give us the exact um 30 percent of the way between those

numbers and we can give it something obscure a little bit more tricky

uh see if the computer can keep up which it can easily obviously

so as i said you don't need to know the maths behind how lab works you just need

to know what it's trying to do finding a point between two numbers unity makes it

super easy to use which i'll show you right now unity

makes it much much easier so look uh this let's make a little variable here

called myvalue and we just call that by math math f dot

lerp it's asking for us for a b and t so our start our goal and our time variable

so let's say 0 and 10 and then let's say the 0.5

and that will return 5 as that is 50 of the way between 0 and 10. so that's how

larp works and unity gives us a bunch of other methods and ways to use lerp but

perform different tasks so let's say we've got two floats here

let's call this one our current and our target and let's say no matter

what target is we always want the our current variable to slowly work towards

becoming target okay so if we set target to 10 we want current to slowly work its

way up to 10 or down if it's higher unity gives us a really easy way to do

that in our update function we can say current is equal to

math f dot move towards now this is using lerp at its core but instead of

giving it a direct t value here we will be giving it a speed

so for this to work we need to give it um what it is currently so our current

we're going to say we want to move towards our target value whatever it is

and then we need a speed variable here so let's create a serialized field so we

can see it in the inspector whoops and let's call this speed make it equal

to something like 0.5 then we'll say speed times time

dot delta time very cool

and we need a way to actually change our target speed or else they're both just

going to sit at zero so we'll say if oops input dot get mess down left click

and we'll set our target is equal to we'll say if our target is currently

equal to zero then let's set it to one otherwise let's set it to zero

and then just to illustrate this let's debug log

our current variable okay head back into unity and let's

attach our script to our cube or our sphere or whatever you're using and uh

let's press play and you'll see

zero if i click it will slowly work its way to one and

then if i click again it will slowly uh lerp its way back down

excellent so we've got this number now that we can control from zero to one in

a nice smooth manner so let's actually do something with it let's say transform

dot position is equal to

and we will need a serialized field here private

whoa what is that vector three

and this will be our goal position holy moly my typing

and so what we're gonna do here is we're going to move our object from its

current position to a goal position and then we can uh ping pong back and forth

and unity gives us a nice little left function here called vector3 lerp and

this is to load vector3s as the name suggests so just the same as

we've got our lerp here we're going to give it the start

position the end position and then the t variable

so our start position is going to be zero because that is where my shape is

on the on the uh on my transform

and the goal will be the goal position and the t will be

current okay because we just made that here

so let's try that and let's set our goal position to 18

and let's go and there you go you're lurping back and

forth on mouth on mouse click very cool uh but we can actually jazz

that up a little bit so that is very linear there's no there's no ease to it

at all so we can add one let's go to serialize field private

oh my goalie that thing keeps showing its face animation curve

curve and just in case you haven't seen that

i'll show you what that looks like in unity

so now on your shape you'll see you've got this curved variable here

and this is what we were using so a very linear path from zero to one

unity provides us these basic ones like this is like a slow ease in and then

ease out so each shape could go a little bit slower speed up and then slow

towards the end and you can also add little points and make something crazy

as well which you should experiment with but i'm just going to use easy in and

out and now instead of just setting up our

time directly to current we can actually use our curve here so curve

dot evaluate and then we'll send in current so this will still go from zero

to one but depending on where our curve is like

what it's doing on the curve it's going to be performing um a faster or slower

action if that makes sense so let's press play

and you'll see that it speeds up and slows down

a little bit more style and pizzazz to it

uh we can do more let's uh rotate it on its way so transform dot rotation is

equal to and just the same way that we did this with vector3 let's do this with

kutonians so it requires a b and t so let's create

a new variable serialize field privates and i'm going to use vectors as it's

easier rotation goal

so our start one will be quaternion euler

zero because that's what we're starting us i believe

yes and the goal will be quaternion euler

goal rotation and

uh we can do current here but as we already set up this curve let's use that

because that will look a little bit sexier

back into unity um and the goal should be something like

let's say okay yeah so about 180 on the x

give it a little bit of spinnies and that didn't work because all i did

was set it there because i'm silly let me just set it here

let's go there we go spins

cool bananas okay so let's add a scale to this as the last little exercise so

we'll do transform scale

equals and as it's a vector we can just do vector three lerp

and we are our start will be i believe one yeah one

and our goal will be whatever the hell we want it to be

so we will serialize our field and let's make this a floats and this will be the

uh goal scale let's default this to two so

we'll double in size so then we'll do one here

times uh goal scale s

goal scale scale goal would have probably been better and then let's use

our little curve here like that

and let's try that out yes very nice but it would probably be

better if it kind of got bigger in the middle and then went back down to its

normal size so let's do that

evaluate curve so instead of it going from zero to one let's

make it go to zero to five and then return its way back down to zero again

so we'll ping pong it and uh unity actually provides a function called math

f dot ping pong

and let's send in our current for t and then length let's make it 0.5

so now that will go up to 5 and then it will wrap and go back down again

let's try that yes

that is all right but it's not actually getting to our scale goal because uh

we're basically dividing it by five so let's

uh multiply that by two let's see how that goes

beautiful how's that it's very nice so i hope you

learned something i hope that was fun to watch if it was leave a like uh maybe

comment tell me what you want to say next and i will see you in the next

episode [Music]

vital tool in your game dev toolkit let's get into it

okay so to get started let's create a script and call it leper or whatever

you'd like to call it it will function the same

okay so before we actually start learning stuff let's learn what lerp

does and i'm just going to do that in javascript because it'll be super easy

for me to show you so given two points let's say a is equal to zero and b is

equal to ten we would like to find a percentage value

between these two numbers so let's say we want to find forty percent of the way

to ten so we can do that with our t for time

and let's just make this 0.4 so we want to find the 40 between 0 and

10. now

uh you don't need to know the maths about about how lerp works you kind of

just need to know like what it does because unity makes it very easy but i'm

just going to glance over the maths right now so let's say our point the

point the 40 point is going to be and this is the formula for lerp

uh it's one minus t so if if t maximum is 1 and we've given

it 0.4 the remainder here will be 0.6 times ra

plus time

multiplied by b now if we console log out our point here

whoops that's not what i wanted you'll see that it is four so that is

forty percent of the way between zero and ten and if we change this to say 3

or 0.33 uh it will

give us the exact um 30 percent of the way between those

numbers and we can give it something obscure a little bit more tricky

uh see if the computer can keep up which it can easily obviously

so as i said you don't need to know the maths behind how lab works you just need

to know what it's trying to do finding a point between two numbers unity makes it

super easy to use which i'll show you right now unity

makes it much much easier so look uh this let's make a little variable here

called myvalue and we just call that by math math f dot

lerp it's asking for us for a b and t so our start our goal and our time variable

so let's say 0 and 10 and then let's say the 0.5

and that will return 5 as that is 50 of the way between 0 and 10. so that's how

larp works and unity gives us a bunch of other methods and ways to use lerp but

perform different tasks so let's say we've got two floats here

let's call this one our current and our target and let's say no matter

what target is we always want the our current variable to slowly work towards

becoming target okay so if we set target to 10 we want current to slowly work its

way up to 10 or down if it's higher unity gives us a really easy way to do

that in our update function we can say current is equal to

math f dot move towards now this is using lerp at its core but instead of

giving it a direct t value here we will be giving it a speed

so for this to work we need to give it um what it is currently so our current

we're going to say we want to move towards our target value whatever it is

and then we need a speed variable here so let's create a serialized field so we

can see it in the inspector whoops and let's call this speed make it equal

to something like 0.5 then we'll say speed times time

dot delta time very cool

and we need a way to actually change our target speed or else they're both just

going to sit at zero so we'll say if oops input dot get mess down left click

and we'll set our target is equal to we'll say if our target is currently

equal to zero then let's set it to one otherwise let's set it to zero

and then just to illustrate this let's debug log

our current variable okay head back into unity and let's

attach our script to our cube or our sphere or whatever you're using and uh

let's press play and you'll see

zero if i click it will slowly work its way to one and

then if i click again it will slowly uh lerp its way back down

excellent so we've got this number now that we can control from zero to one in

a nice smooth manner so let's actually do something with it let's say transform

dot position is equal to

and we will need a serialized field here private

whoa what is that vector three

and this will be our goal position holy moly my typing

and so what we're gonna do here is we're going to move our object from its

current position to a goal position and then we can uh ping pong back and forth

and unity gives us a nice little left function here called vector3 lerp and

this is to load vector3s as the name suggests so just the same as

we've got our lerp here we're going to give it the start

position the end position and then the t variable

so our start position is going to be zero because that is where my shape is

on the on the uh on my transform

and the goal will be the goal position and the t will be

current okay because we just made that here

so let's try that and let's set our goal position to 18

and let's go and there you go you're lurping back and

forth on mouth on mouse click very cool uh but we can actually jazz

that up a little bit so that is very linear there's no there's no ease to it

at all so we can add one let's go to serialize field private

oh my goalie that thing keeps showing its face animation curve

curve and just in case you haven't seen that

i'll show you what that looks like in unity

so now on your shape you'll see you've got this curved variable here

and this is what we were using so a very linear path from zero to one

unity provides us these basic ones like this is like a slow ease in and then

ease out so each shape could go a little bit slower speed up and then slow

towards the end and you can also add little points and make something crazy

as well which you should experiment with but i'm just going to use easy in and

out and now instead of just setting up our

time directly to current we can actually use our curve here so curve

dot evaluate and then we'll send in current so this will still go from zero

to one but depending on where our curve is like

what it's doing on the curve it's going to be performing um a faster or slower

action if that makes sense so let's press play

and you'll see that it speeds up and slows down

a little bit more style and pizzazz to it

uh we can do more let's uh rotate it on its way so transform dot rotation is

equal to and just the same way that we did this with vector3 let's do this with

kutonians so it requires a b and t so let's create

a new variable serialize field privates and i'm going to use vectors as it's

easier rotation goal

so our start one will be quaternion euler

zero because that's what we're starting us i believe

yes and the goal will be quaternion euler

goal rotation and

uh we can do current here but as we already set up this curve let's use that

because that will look a little bit sexier

back into unity um and the goal should be something like

let's say okay yeah so about 180 on the x

give it a little bit of spinnies and that didn't work because all i did

was set it there because i'm silly let me just set it here

let's go there we go spins

cool bananas okay so let's add a scale to this as the last little exercise so

we'll do transform scale

equals and as it's a vector we can just do vector three lerp

and we are our start will be i believe one yeah one

and our goal will be whatever the hell we want it to be

so we will serialize our field and let's make this a floats and this will be the

uh goal scale let's default this to two so

we'll double in size so then we'll do one here

times uh goal scale s

goal scale scale goal would have probably been better and then let's use

our little curve here like that

and let's try that out yes very nice but it would probably be

better if it kind of got bigger in the middle and then went back down to its

normal size so let's do that

evaluate curve so instead of it going from zero to one let's

make it go to zero to five and then return its way back down to zero again

so we'll ping pong it and uh unity actually provides a function called math

f dot ping pong

and let's send in our current for t and then length let's make it 0.5

so now that will go up to 5 and then it will wrap and go back down again

let's try that yes

that is all right but it's not actually getting to our scale goal because uh

we're basically dividing it by five so let's

uh multiply that by two let's see how that goes

beautiful how's that it's very nice so i hope you

learned something i hope that was fun to watch if it was leave a like uh maybe

comment tell me what you want to say next and i will see you in the next

episode [Music]

Tom'ZzZaurinTarodevMayank BhaisoravandendeJabajaba JaLuu InternationalTarodevLuu InternationalTarodevAndrew SneackerTarodevJustin CarperNicolay EdinStewart tStewart tTarodevKenneth BaileyMahmud Mizan FahimДмитрий БолденкоAlvaro FredericoSuyash MehrarelentingVidsGregory FenncoffeecoffeeGregory Fenniamblinkin