Sometimes when making a game you'll need
a smooth transition between two values.
A common example is smoothing a change
in position. You can do this using a
function called Lerp. Lerp is short for
Linear Interpolation. In this scene we have
a light and script that lerps three
properties of the light. It's position,
colour and intensity.
Let's take a look at how we use lerp for
each of these properties.
First off, position. In this unfinished
example I have a variable to store
a new vector3 position
holding X, Y and Z
In Awake I set this value to the current
position of the object by using
transform.position.
Then in update we're running a position changing
function every frame.
So what happens here? Well we have
two more vector3 variables position A
and position B. These are two positions
that I want to switch the light between.
Then I simply check for input when I
press the key Q,
I move to position A. And when I press
the key E I move to position B.
And you can see here I'm setting the current
position to this variable and constantly
changing between it. So let's see how that works.
If I press Q I move here, -5 in X.
And when I press E I get positive 5.
But, as you can see, we're just jumping
between those two positions, which is not
what we want. So this is where we use a Lerp
to smoothly interpolate between
those two values.
So instead of setting transform position
to the value of new position
we're going to say vector3.lerp.
And the basic properties of a lerp are
threefold. The 'from' value, the 'to' value
and a 'time' value.
So the starting value of this, because
it's the position will be the current
transform.position. That's the 'from' value.
Then we'll set the 'to' value
with the variable newPosition.
And finally we need a value for 'time'.
So we'll use Time.deltaTime.
Let's see how this looks.
Now you can see we're smoothly interpolating
between those two points.
We might want to increase the speed at
which this occurs, so we could add in
a variable to help us do this.
I am going to add in a public float
and I am going to call it smooth and
I will start off with a value of 2.
Then I will simply multiply my delta time
by that value.
And now you can see our interpolation
between those two points is a lot quicker.
And because it's a public variable we
can crank up the speed and do it
nice and fast too.
Now let's look at changing the intensity.
This once is slightly different because
we're using Mathf.Lerp rather than using
vector3.Lerp. Mathf.Lerp will go between
two float values rather than going between
two vector3 values.
So if you have something that's using a
float you would used Mathf.Lerp, and something
that is using a vector3 you'd use
vector3.Lerp. This time around we've setup
our code for changing the intensity
of the light. We have a new intensity
variable. We set it equal to the light
intensity at the start of the game
and we're running intensity changing every frame.
This time we have two different intensities
0.5 and positive 5.
And then we're using the keys A and D
to set between these two intensity values.
So you can see that we're simply addressing
the light component, it's intensity property
and setting it equal to a Mathf.Lerp.
The 'from' value is the current intensity
and the value to interpolate towards
is the value of the new intensity variable.
And yet again we are using Time.deltaTime
multiplied by our smoothing variable.
Let's see how this looks.
And as usual if our smoothing value differs
we can increase the speed at which this
interpolation occurs.
Finally let's look at interpolating the colour.
This time around we have setup a new colour
variable at the top. We have set it equal to
the light component's colour value when
the game starts. And yet again we have
a function that is running every frame.
We have two colour shortcuts
Colour.red and Colour.green
stored in A and B variables
and we're switching them with Z and C
on the keyboard. As with our other lerps
we have a 'from' and a 'to' value,
the original light.colour and the new colour.
And again we're using Time.deltaTime
multiplied by smooth.
Simple. So when I press play
I can move between those values.
And if I want to increase the speed
I crank up my smooth value.
Let's take a look at the finished scene
with all of those included.
Subtitles by the Amara.org community