- Home /
How to make game object move every 10 seconds?
I would like my game object to move up after 10 seconds, then move down after 10 seconds, back and forth like that. Right now it only moves up after 10 seconds, I don't know how to make it so it goes back down after another 10 seconds and back and forth like that every 10 seconds.
{
[SerializeField]
Transform[] waypoints; //way point path of game object
[SerializeField]
float moveSpeed = 3f;
int waypointIndex = 0;
//DON'T HAVE THE GAME OBJECT MOVE RIGHT AWAY, WAIT 10 SECONDS
private bool isWait = true;
void Start()
{
StartCoroutine(StartDelay());
transform.position = waypoints[waypointIndex].transform.position;
}
private void Update()
{
//AFTER 10 SECONDS THE GAME OBJECT STARTS MOVING
if (!isWait)
{
Move();
}
//AFTER 3 SECONDS, STOP THE MOVEMENT
if (isWait)
{
StartCoroutine(StopObject());
}
}
private void Move()
{
transform.position = Vector3.MoveTowards(transform.position, waypoints[waypointIndex].transform.position, moveSpeed * Time.deltaTime);
if (transform.position == waypoints[waypointIndex].transform.position)
{
waypointIndex += 1;
}
if (waypointIndex == waypoints.Length)
waypointIndex = 0;
}
//after 10 seconds, the object moves
IEnumerator StartDelay()
{
yield return new WaitForSeconds(10f);
isWait = false; //object is no longer waiting to move
}
//once the object starts moving, it stops moving after 3 seconds
IEnumerator StopObject()
{
yield return new WaitForSeconds(15f);
isWait = true; //object stopped moving
}
}
Answer by Tondoy · Feb 21 at 03:55 PM
I think I fixed your issue. The solution was to restart the coroutine, whene the object reached the waypoint. The code now looks like this:
{
[SerializeField] Transform[] waypoints; //way point path of game object
[SerializeField] float moveSpeed = 3f;
int waypointIndex = 0;
//DON'T HAVE THE GAME OBJECT MOVE RIGHT AWAY, WAIT 10 SECONDS
private bool isWait = true;
void Start()
{
StartCoroutine(StartDelay());
transform.position = waypoints[waypointIndex].transform.position;
}
private void Update()
{
//AFTER 10 SECONDS THE GAME OBJECT STARTS MOVING
if (!isWait)
{
Move();
}
}
private void Move()
{
transform.position = Vector3.MoveTowards(transform.position, waypoints[waypointIndex].transform.position, moveSpeed * Time.deltaTime);
if (transform.position == waypoints[waypointIndex].transform.position)
{
waypointIndex += 1;
StartCoroutine(StartDelay()); // restarts the coroutine
}
if (waypointIndex == waypoints.Length)
waypointIndex = 0;
}
//after 10 seconds, the object moves
IEnumerator StartDelay()
{
isWait = true;//object now waits to move
yield return new WaitForSeconds(2);
isWait = false; //object is no longer waiting to move
}
}
Your answer
Follow this Question
Related Questions
My gameobject only moves to the right once and then stops, why? 3 Answers
How do i make a cube move (Continuosly without stopping) when i press a button once in unity 2D 2 Answers
Enemy shakes on Y-axis when is moving straight on the X-axis 0 Answers
Using UI buttons for Input Manager 0 Answers
How Time.deltaTime affects movement ? 4 Answers