Search Unity

Trouble Interpolating between two Vectors

Discussion in 'Scripting' started by littlelingo, Oct 27, 2006.

  1. littlelingo

    littlelingo

    Joined:
    Jul 18, 2006
    Posts:
    372
    I have a gameObject and got a point from a Raycast hit I am looking to move the GameObject for it's current position to the point over time.

    Can someone please point me in the right direction?

    Thanks for the help!

    -- Clint
     
  2. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    Code (csharp):
    1.  
    2. var object : GameObject;
    3. var oldPosition : Vector3;
    4. var hit : RaycastHit;
    5. var moveSpeed = 1.00;
    6.  
    7. private var time = 0.00;
    8.  
    9. // do this once the raycast is done. remember to reset the time if this is to be done more than once.
    10.  
    11. oldPosition = object.transform.position;
    12. time = 0;
    13.  
    14. function Update () {
    15.    
    16.     if(time != 1) {
    17.  
    18.         time += Time.deltaTime * moveSpeed;
    19.         object.transform.position = Vector3.Lerp(oldPosition, hit.point, time);
    20.         if(time >= 1) time = 1;
    21.     }
    22. }
    23.  
     
  3. littlelingo

    littlelingo

    Joined:
    Jul 18, 2006
    Posts:
    372
    Excellent, Lerp is what I was looking for!

    Thanks for the help!

    -- Clint