Monster shadow

VIDEO

Lerping in Unity - You HAVE to know this!!

Description:
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! ========= SUBSCRIBE: https://bit.ly/3eqG1Z6 DISCORD: https://discord.gg/GqeHHnhHpz MORE TUTORIALS: https://www.youtube.com/tarodev
ADD A COMMENT

Dan Pos - Game Dev Tutorials!
Excellent stuff, one of the issues I've always had with controlling lerps is how best to manipulate t but this made a lot of sense to me
Breepus Kranston
This helped soooo much, thank you. I didn't know the curve animations existed, and I was trying to figure out a way to curve the values. Now I know!!
Tarodev
Endless possibilities now 😉
Monkok
Cool video, helped me better understand lerp. How would you combine it with character controller Move()?
Tarodev
If you use Move(direction * _speed * Time.DeltaTime), it will give you movement. Is that what you're after?
Dhiraj Agrawal
This is probably one of the most precise yet comprehensive explanations I have seen of using Lerp in Unity. Subscribed and hope you continue making more
Tarodev
High praise. Thank you :)
Makra
Life is so much smoother with lerp :)
Tarodev
Smooth like butter
MasthaX
nextpos = Vector3.Lerp(pos,somevector, t); my favourite..
THE EARTH BURNER
So helpful
Brani_Dev
Nice never use lerping i think before or maybe i did in some tutorials but this video was cool to watch you should definitely do more of them
Tim Heinlein
Very informative! Great stuff.
JackFoz454
Cheers for this! Your videos are great. Even when I know the topic, I enjoy watching and invariably pick up something cool anyway.
Tarodev
Glad I can keep things fresh for you 👍
Crazy car sim
hey i came across your channel recently ...can you make a beginner unity and c# 2021 tutorial ??your videos are awesome and are very good quality...can you make some beginner tutorials pls???a playlist also
Crazy car sim
@Tarodev thank you so much
Tarodev
I'm currently making a course. It should be done in a month :)
Nikos Paraponiaris
Nice Tutorial! <3 - I am really wondering if it is able to use this in a 2D Player mechanic - A guns angle follows Mouse Position (But with Delay) - But i am noob to try it.. because i guess it has to be done with vectors.. screen camera mouse position etc.. etc..
Nikos Paraponiaris
@Tarodev Thanx for Reply!
Tarodev
This is EXACTLY what you'd use lerp for. Any kind of movement (not physics based) can be done using lerp. You'll find you use it everywhere :)
Oya Guni
I got introduced to lerp yesterday and this video has been really helpful to see more use cases and to help me understand a little more the concept, thanks a lot as always!
Tarodev
Glad I could help :)
Jason Moreland
Great stuff
Mateusz
Very nice video 😊 Maybe you will create something with bezier curves? Path/moves/animations?
Tarodev
I think I'll do just that 😊
Deeann giffard
vaw.fyi cool
Brian Purdy
Great video! I really enjoy your content and a good Segway point if you were to work your way to a Bezier Curve tutorial. I'd love to see your take on a pattern or 2 (dealers choice of course on which) just love learning new things or new ways to achieve the things I may already know. Cheers!
Brian Purdy
@Tarodev yes I do, but since i'm working on a capstone project most of my meetings are game features or bug related. 😄
Tarodev
@Brian Purdy excuse me sir, should you be game deving whilst in a meeting? 😂
Brian Purdy
@Tarodev yes, sorry I was in the middle of a meeting when I did that and the app I used has low graphic quality as I am not setup to do recording atm. Love the content, as well as the quality within!
Tarodev
@Brian Purdy ahhh you meant design patterns! Yes that's a great idea actually. Thanks for the little video 😄
Brian Purdy
@Tarodev some patterns are more common then others, first one that comes to mind if the component pattern which unity uses extensively but the observer pattern is very useful in certain games. Here I uploaded a video that I think is a decent idea of maybe how an RTS game could use this pattern, check it out if you like updated it just for this reply haha. Observer Pattern attempt by me https://youtu.be/pLm1_HOdiXk
Roy [ Random Precision Software ]
why use Java in unity?, it used C# now
Tarodev
I used Javascript (not in unity) so that while I explained the lerp formula it would compile in real time to show results. All the actual unity code in this video is done in C#.
datablob
Lerp is so fascinating, it blew my mind when I finally understood how fading between colours is also Lerp under the hood.
Tarodev
You can do so much with lerp! Plus it's super handy in shaders for (as you said) colors, gradients, etc.
datablob
or bezier curves!
Chahd Studio
very good tutorial as always, but still waiting for a tutorial to achieve a look and feel similar to your projects
Future Trunks
@Tarodev 🙏🏻
Chahd Studio
@Tarodev thank you man
Tarodev
I promise, it's coming :) I know you're waiting for it.
SEE ALL COMMENTS


Transcript:

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]