Move objetc with multiple vectors and Force

Hi,

I have created a List vecs = new List();
and filled it with 3 vectors that I want to use in a AddForce.

What I want is to have my object to move for like 4 seconds on every vector with a stop inbetween every vector.

I have tried while-loops, WaitForSecondsRealtime but without any luck… All of the methods i tried just makes my object to move with my last vector in the List…
I have read a lot of threads without finding any answer to my problem…

So is there anyone who have done this and can guide me in the right direction?

Regards
Rikard

Something like this? (Not tested)

int index = 0;
float timer = 0f;
bool doDelay = false;
List<Vector3> vecs = new List<Vector3>( { <insert values here> } );

void Update()
{
    timer += Time.deltaTime;
    if (doDelay)
    {
        DoDelay();
    }
    else
    {
        Move();
    }
}

void DoDelay()
{
    if (timer >= 2f) // TODO: Replace hard-coded value with variable
    {
        timer -= 2f;
        doDelay = false;
    }
}

void Move()
{
    if (timer >= 4f) // TODO: Replace hard-coded value with variable
    {
        timer -= 4f;
        index++;
        if (index >= vecs.Count)
        {
            index = 0;
        }
        doDelay = true;
    }
    else
    {
        rigidbody.AddForce(vecs[index]); // Reference to rigidbody not included here
    }
}
1 Like

Thank you so much!!
That was what I needed to get me going…

/Rikard

1 Like