Help with timers

So I am trying to make an object move back in C#:

transform.Translate(Vector3.back * Time.deltaTime, Space.World);

However, I only want it to move backwards for a certain amount of time. How would I go about this? BTW I am a massive newbie with this stuff if you couldn’t already tell.

You could set a variable like :
private float finishTime;
private bool backing= false;

Then in you function
void moveBack(){
backing = true;
finishTime = Time.time + 1.00;
}

void update(){
if(backing){
if(Time.time < finishTime)Translate()
else backing = false;
}
}

I would suggest a CoRoutine which is triggered maybe in our Update method and runs for a given time and moves the GameObject:

float backwardsTime = 5.0f;

void Update()
{
  // move backward condition is fulfilled
  StartCoroutine("MoveBackwards", backwardsTime);
}

IEnumerator MoveBackwards(float moveTime)
{
  // Calculate the finish time
  float finished = Time.time + moveTime;
  while (Time.time < finished)
  {
    // Move backwards
    transform.Translate(Vector3.back * Time.deltaTime, Space.World);

    // And leave for the next iteration
    yield return new WaitForSeconds(0.02f);
  }
}

I’d suggest the same as THoeppner, except:

// And leave for the next iteration
yield return new WaitForSeconds(0.02f);

Should be:

// And leave for the next iteration
yield return null;

Except starting new coroutines every frame is probably not intended behavior… I’d move it to start or awake instead of update.

Hm, maybe my Update method was a little bit ambiguous :wink:

The comment in the Update method ment that you should start the coroutine only when the conditions are fulfilled. Because I don’t know these conditions I just wrote the comment. Maybe better is to write:

void Update()
{

  if (move backward condition is fulfilled)
  {
    StartCoroutine("MoveBackwards", backwardsTime);
  }
}

A coroutine that cycles with the update loop is pretty senseless.

personally, I would do it thusly

bool _moveBackwards

void Start()
{
  MoveBackwardsToggle();
}

void Update()
{
  if(_moveBackwards)
     transform.Translate(-transform.forward * Time.deltaTime);
}

void MoveBackwardsToggle()
{
  _moveBackwards = !_moveBackwards;

  Invoke("MoveBackwardsToggle", 2);
  
}

there is a downside to this method, that is if you call Toggle before its Toggled itself, it will get confused… if you want a safer method that allows multiple calls to moving backwards…

float _moveBackTimer = 0;

void Update()
{
  if(_moveBackTimer > 0)
  {
     transform.Translate(transform.forward * -Time.deltaTime);
    _moveBackTimer -=Time.deltaTime;
  } else _moveBackTimer = 0;
}

void MoveBackwards()
{
  _moveBackTimer += 2;
}

personally, I would do it thusly

bool _moveBackwards

void Start()
{
  MoveBackwardsToggle();
}

void Update()
{
  if(_moveBackwards)
     transform.Translate(-transform.forward * Time.deltaTime);
}

void MoveBackwardsToggle()
{
  _moveBackwards = !_moveBackwards;

  Invoke("MoveBackwardsToggle", 2);
  
}

there is a downside to this method, that is if you call Toggle before its Toggled itself, it will get confused… if you want a safer method that allows multiple calls to moving backwards…

float _moveBackTimer = 0;

void Update()
{
  if(_moveBackTimer > 0)
  {
     transform.Translate(transform.forward * -Time.deltaTime);
    _moveBackTimer -=Time.deltaTime;
  } else _moveBackTimer = 0;
}

void MoveBackwards()
{
  _moveBackTimer += 2;
}