transfrom.translate - timer

Hi, I am using this script to move an animation with no root-motion. it simply walks across the terrain. This is the code below.

using System.Collections;
using UnityEngine;

public class Mover : MonoBehaviour {

	void Update () 
	{
		transform.Translate(0, 0, Time.deltaTime);
		// this would add the delta time each frame to the z position of the object this script is attached to
	}
}

I want introduce some kind of timer to it, so it doesn’t move straight away but waits for about 60 seconds and then starts moving. Maybe something like ‘WaitForSeconds’? not sure, Whats the easiest way to do this? thanks

You could use Time.time:

if (Time.time > 60)
{
         transform.Translate(0, 0, Time.deltaTime);
}

Hi @ManuelMeyer
Great many thanks, the first way Time.time does the job perfectly!
If I also wanted it to just travel a certain distance and then stop is there a very simple way to add this? thanks

The answer given by @ManuelMeyer will not work in most cases: Time.time is the time in seconds since the start of the game. If you add a main menu, and the player takes more than 60 seconds to access to your scene, then your animation will start to move right away!

Instead, you should use the Invoke function: (Invoke - Unity Learn) or a coroutine, which is cleaner.

If you have trouble with coroutines, you can check out my script: GitHub - SebastienAwali/SAction: A simple Unity C# script that wraps Unity's Coroutines, easily allowing developers to create and manage actions over time.

Hi @s_awali Yes it works fine in the individual scene but when I navigate to that scene from another, the timer has already played.
I thought DontDestroyOnLoad may sort this out? but not sure, will look at your example over the w/end when I get a chance. thanks