The question is answered, right answer was accepted
How to use Vector2.Lerp in two arrays
My question is mostly in the title. I’m stuck because I want to lerp the whole Vector2 a with the entire Vector2 b. Sorry for my english, hope you guys will understand !
Vector2[] vertices2D1 = new Vector2[] {
new Vector2(0,0),
new Vector2(54.7f,53.9f),
new Vector2(77.3f,141.8f),
new Vector2(77.3f,327.6f),
new Vector2(54.7f,428.1f),
new Vector2(0,500),
};
For example I can change the first element of my first array with vertices2D [0].x = 10f; vertices2D [0].y = 15f; but it becomes instantly (no transition), how use this with void update .And i can't use lerp with this syntax for example Vector2.Lerp(vertices2D [0].x, "here my new vector" ,0.3f); Thank you
Answer by Hellium · Apr 24, 2018 at 03:00 PM
I don't really understand your problem....
For example, if you want an object to go smoothly from 1st vector, to 2nd one, and so on, you can do the following:
public float speed = 1;
Vector2[] vertices2D1 = new Vector2[] {
new Vector2(0,0),
new Vector2(54.7f,53.9f),
new Vector2(77.3f,141.8f),
new Vector2(77.3f,327.6f),
new Vector2(54.7f,428.1f),
new Vector2(0,500),
};
int currentIndex = 0 ;
float progress = 0 ;
Vector3 startPosition ;
Vector3 endPosition ;
void Start()
{
startPosition = transform.position ;
endPosition = vertices2D1[currentIndex];
}
void Update()
{
progress += Time.deltaTime * speed ;
transform.position = Vector2.Lerp( startPosition, endPosition, progress ) ;
if( progress >= 1 )
{
progress = 0 ;
currentIndex = ( currentIndex + 1 ) % vertices2D1.Length ;
startPosition = endPosition ;
endPosition = vertices2D1[currentIndex] ;
}
}
If you want to smoothly change the 1st vector of the array:
public float speed = 1;
Vector2[] vertices2D1 = new Vector2[] {
new Vector2(0,0),
new Vector2(54.7f,53.9f),
new Vector2(77.3f,141.8f),
new Vector2(77.3f,327.6f),
new Vector2(54.7f,428.1f),
new Vector2(0,500),
};
float progress = 0 ;
void Update()
{
progress += Time.deltaTime * speed ;
vertices2D1[0] = Vector2.Lerp( vertices2D1[0], <your_target_vector_here>, progress ) ;
}
Oh, thank you ! Yes the second answer is good ;) i have two array the first :
Vector2[] vertices2D1 = new Vector2[] {
new Vector2(0,0),
new Vector2(54.7f,53.9f),
new Vector2(77.3f,141.8f),
new Vector2(77.3f,327.6f),
new Vector2(54.7f,428.1f),
new Vector2(0,500),
};
and the second :
Vector2[] vertices2D2 = new Vector2[] {
new Vector2(20,10),
new Vector2(50f,8f),
new Vector2(6f,10.8f),
new Vector2(3f,300.6f),
new Vector2(50.7f,418.1f),
new Vector2(0,20),
};
And i want to change all the vector in vertices2D1 array with all the vector in vertices 2D2 array (smoothly)
Follow this Question
Related Questions
Lerp a vector2?,Lerp a Vector2? 0 Answers
Vector2.lerp dashing problem 0 Answers
Question about Array indexing 0 Answers
Help with Vector2.Lerp 2 Answers
Lerping at a constant rate? 2 Answers