How to compared time spacing wanted with actual time elaspsed

I have an object moving accross the screen and ever 30 seconds i want the object to stop moving moving for 2-3 seconds and then start moving again. I have been trying to find a way to compare the time spacing i want with the actual time elapsed. But im coming up blank. I was using
the code

if(30 == Time.time)
{
  Object stop moving;
}

//thats only sudo code.

but that that dosent work becuase Time.time is read only. So i want to know if there is another command that i am missing that i can use to compare the time spacing i want with the actual amount of since the game started. or am i just going about this totally the wrong way.

1 Answer

1

You need to stock the time when your object starts moving :

var startTimer:float = Time.time;

And then in the Update() method, you can have :

if ( Time.time >= startTimer + 30 )
{
    StopMyObjectFor(3.0);
}

You also need to declare the function StopMyObjectFor :

function StopMyObjectFor(waitingTime:float)
{
    yield WaitForSeconds(waitingTime);
    // Now you start moving your object again here

    // now we need to start over the counter :
    startTimer = Time.time;
}

This is in Javascript of course.

Cheers thanks, ive only wasted the last 2 days trying to figure out something that turned out to be so simple.

No problem :) Please accept my answer as right answer if it helped !

Sorry im new to this, how do i accept an answer as right?

There is a check mark on the left of my answer. The thumbs up is to mark an answer/comment that helped (you can thumbs up on a topic that you didn't open, but you can't mark an answer as good answer if you are not the one who asked)