Plugin: added support to our Vector Field p. Returns the 2D vector perpendicular to this 2D vector. The result is always rotated 90-degrees in a counter-clockwise direction for a 2D coordinate system where the positive Y axis goes up. Unity has a Vector2.Distance method that removes the need for us to do any vector math: float distance = Vector2.Distance( transform.position, target.position ); But we’re more interested in understanding the math behind it here. C# / Unity Vector2 rotation 2015-03-07 03:20:54 Making a simple asteroids type game to get more comfortable with C# and Unity. I have the asteroids falling from the top of the screen after I instantiate them randomly from spawn points and it all works great. Easy fix: You have to use new Vector2 (float, float) for that to work. Same with any other type. Alternatively, use Vector2.right.-1 or -Vector2.right.
Unity Vector2 Magnitude
Welcome to this Unity 2D Lerp Tutorial. The Unity 2D Lerp function is often used to move objects along a path or to change values along a path.
However in essence LERP is code word for Linear Interpolation, but what is linear interpolation?
Well interpolation is a mathematical concept which is used to fit points within other points.
Essentially how this works is it takes a set of points and estimates other points for a given x or y value on a graph.
Visually explaining Lerp
Here is essentially what this would look like.
To explain this in brief. The green dots are all the co ordinates that are known.
The red one is a estimation of what the point will look like at that given x or y. So how Lerp works in unity is it will use the first point you give it and the 2nd point you give.
It and create estimations for the time value you pass in. By doing so you can get linear values back between the point 1 and point 2.
With these values you can modify scales, object positions etc, anything that has a linear motion or change.
Here is what the Lerp function definition looks like.
public static Lerp(Vector2 a, Vector2 b, float t);
So in terms of our graph Vector2 a could be the first green dot at the bottom of our graph and Vector2 b could be our last one at the top.
The float t, will move us along on the x axis and give us estimates of the line drawn between Vector a and Vector b.
In so doing if we step the time in our Lerp function we would be able to animate, move or modify unity game object.
I hope this makes sense. Can be quite tricky without a mathematical background.
You might begin to understand it more in depth with the following demonstrations.
Unity Vector2 C#
Demonstrating unity lerp between two positions
Let’s start off with a new unity project. Open up unity hub and create a project called unity 2d lerp tutorial.
So generally when we say translate in the same sentence as lerp we talking about different things really.
Lerp will give us the next co ordinate in our series, where translate will add values to our vector.
It technically would be possible to simulate a lerp with translate. By using a linear function to determine the gradient of our linear graph.
However let us stick to the lerp way of doing a “translate”. This will be using the time function. Let’s start by adding a basic square to our scene.
So in the assets tab add a square 2d sprite like this.
Drag it into your hierarchy.
Let’s now create the first basic c# script to handle a “translate”. Go ahead and right click in the assets tab and create a new c# script and call it translate lerp.
Open that up in visual studio code. Here is what our code is going to look like.
Save that off and attach that to our square by dragging it into the insepector.
Run that and you should get a square moving on the screen. So very simple how this works.
Indesign 2014 version. First we have our two vectors point 1 and point 2. We then lerp between the two passing the lerp function our deltaTime * 1.1f. The 1.1f which is our speed at which we want to move our object.
We assign this to a accum variable which is our lerp float over time.
Modifying this code to lerp scale
All we now need to do to get unity lerp to scale is change the code to have this instead.
Unity lerp smooth step
Want to smooth out the motion try this.
Here we use Mathf.SmoothStep to control the estimation.
That’s it for this tutorial. Going to end it here and in the next section I will be answering some frequently asked questions.
How do I do a smooth lerp in unity?
To do a smooth lerp you can use a smoothing function like Mathf.SmoothStep.
Unity Vector2 Distance
How do I do lerp on scale in unity?
To lerp on scale in unity you can simply provide 2 vectors and lerp over the localScale like this.
accum += 1.1f * Time.deltaTime;
this.transform.localScale = Vector2.Lerp(p1,p2, accum);
How do I do a translate lerp in unity?
You can lerp between two points like this.
accum += 1.1f * Time.deltaTime;
this.transform.position= Vector2.Lerp(p1,p2, accum);
Some final words
Unity Vector2 Lerp
I hope this tutorial was useful and helped you understand lerp in a new way. If you want to support me and my content please check out my skillshare course here:
Unity Vector2int
If you prefer watching video over reading tutorials, why not subscribe to my YouTube channel over here: https://www.youtube.com/channel/UC1i4hf14VYxV14h6MsPX0Yw?sub_confirmation=1
Unity Vector2 Api
Here are also some other tutorials you might be interested in: