Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

Store array of points between 2 objects

Discussion in 'Scripting' started by Danz0r77, Apr 14, 2013.

  1. Danz0r77

    Danz0r77

    Joined:
    Mar 19, 2013
    Posts:
    20
    Hi

    I'm probably being really stupid here but I can't figure out how I do this.

    I have two points in my game - origin and destination. I want to do a kind of Lerp to get an array of x amount of Vector3 positions between those 2 points.

    Something like:

    Code (csharp):
    1. location1 = Vector3(0,0,0);
    2. location2 = Vector3(10,0,0);
    3. arrayOfPoints = Lerp(Vector3(location1,location2,1.0);
    So the results would be something like an array of 10 Vector3 values:
    (0,0,0)
    (1,0,0)
    (2,0,0)
    (3,0,0)
    etc...

    I'm not sure if Lerp is what I should be using to do that. Is there any way?

    Thanks,
    Dan
     
  2. Diviner

    Diviner

    Joined:
    May 8, 2010
    Posts:
    677
    Code (csharp):
    1.  
    2. Vector3 location1 = Vector3.zero;
    3. Vector3 location2 = new Vector3(10,0,0);
    4. Vector3[] arrayOfPoints = new Vector3[10];
    5.  
    6. int i = 0;
    7. for(float y = 0.0f; y < 1.0f; y += 0.1f) {
    8.  
    9.           arrayOfPoints[i] = Vector3.Lerp(location1, location2, y);
    10.           i++;
    11.  
    12. }
    13.  
     
  3. Danz0r77

    Danz0r77

    Joined:
    Mar 19, 2013
    Posts:
    20
    Ahh, nice one.

    That's perfect - thanks. :)
     
unityunity