0
\$\begingroup\$

I was working on Lerp on arrays of Vector3, i want this function to work without update function(like using loops) i am not getting how to implement it.

public class MoveTowards : MonoBehaviour {

    int currentStartPoint;
    private Vector3 startMarker, endMarker;
    public float speed = 1.0F;
    private float startTime;
    private float journeyLength;
    public float smooth = 5.0F;
    List<Vector3> tempPositionList = new List<Vector3>();
    List<Vector3> positionList = new List<Vector3>
        {
            new Vector3(4,0,0),
            new Vector3(4,0,2),
            new Vector3(4,0,4),
            new Vector3(2,0,4),
            new Vector3(0,0,4),
            new Vector3(-2,0,4),
            new Vector3(-4,0,4),
            new Vector3(-4,0,2),
            new Vector3(-4,0,0),
            new Vector3(-4,0,-2),
            new Vector3(-4,0,-4),
            new Vector3(-2,-0,-4),
            new Vector3(0,0,-4),
            new Vector3(2,0,-4),
            new Vector3(4,0,-4),
            new Vector3(4,0,-2),
            new Vector3(2,0,-2),
            new Vector3(0,0,-2),
            new Vector3(-2,0,-2),
            new Vector3(-2,0,0),
            new Vector3(-2,0,2),
            new Vector3(0,0,2),
            new Vector3(2,0,2),
            new Vector3(2,0,0),
            new Vector3(0,0,0)
        };
    void Start()
    {
        currentStartPoint = 0;
        SetPoints();
    }
    public void SetPoints()
    {
        startMarker = positionList[currentStartPoint];
        endMarker = positionList[currentStartPoint + 1];
        startTime = Time.time;
        journeyLength = 2;
    }

    void Update()
    {
        Debug.Log(positionList.Count);
        float distCovered = (Time.time - startTime) * speed;
        float fracJourney = distCovered / journeyLength;
        transform.position = Vector3.Lerp(positionList[currentStartPoint], positionList[currentStartPoint + 1], fracJourney);
        if (fracJourney >= 1f && currentStartPoint + 1 < positionList.Count)
        {
            currentStartPoint++;
            SetPoints();
        }
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ Why don't you want to use the Update function? \$\endgroup\$
    – Philipp
    Commented Oct 4, 2017 at 8:18
  • \$\begingroup\$ because i want to call it from another class and get values from that class of the start position and end position. \$\endgroup\$ Commented Oct 4, 2017 at 8:20

1 Answer 1

1
\$\begingroup\$

You can use Coroutines if you don't want to use the Update function.

void Start()
{
    journeyLength = 2 ;
    StartCoroutine( Travel() ) ;
}

private IEnumerator Travel()
{        
    for( currentStartPoint = 0 ; currentStartPoint < positionList.Count - 1 ; ++currentStartPoint )
    {
        Debug.Log( "Going to : " + positionList[currentStartPoint + 1] );
        for( float t = 0 ; t < journeyLength ; t += Time.deltaTime )
        {
            float alpha = t / journeyLength ;
            // Or use the following line if you want smooth movement :
            // float alpha = Mathf.SmoothStep(0, 1, t / journeyLength);
            transform.position = Vector3.Lerp(positionList[currentStartPoint], positionList[currentStartPoint + 1], alpha);
            yield return null ;
        }
        transform.position = positionList[currentStartPoint + 1];
    }    
}

But keep in mind that coroutines can't be started if your GameObject is disabled

\$\endgroup\$
2
  • \$\begingroup\$ its not working the same its directly going to the last position \$\endgroup\$ Commented Oct 4, 2017 at 7:35
  • \$\begingroup\$ My bad, I've forgotten to initialize the value of journeyLength to 2. \$\endgroup\$
    – Hellium
    Commented Oct 4, 2017 at 7:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .