I'm new to Unity, and am trying to find a way to move a block forward for a set amount of time, then have it travel back to its starting position in that same amount of time. I'm using Time.deltaTime in order to move the block for a set amount of time, and this does work. However, once the countDown variable reaches 0 and the object has to start travelling back to its original position, the object stops moving and I'm not sure why.
public class Problem1 : MonoBehaviour {
float countDown = 5.0f;
// Use this for initialization
void Start () {
}
void Update () {
transform.position += Vector3.forward * Time.deltaTime;
countDown -= Time.deltaTime;
if (countDown <= 0.0f)
transform.position += Vector3.back * Time.deltaTime;
}
}
I'm fairly certain that I'm using Vector3.back incorrectly, but I can't figure out how.