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
}
}