How to stop object from transform.position and wait for few seconds then transform.position again?

I’m trying to move my object using gameObject from Point A to Point B then waiting for few seconds then move my object again.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class movingCubus : MonoBehaviour {


    public Transform[] target;
    public float speed;
    public int current;


    void Start(){
      
        StartCoroutine(Example());
    }
   

    // Update is called once per frame
    void Update () {


            if (transform.position != target[current].position){


            Vector3 pos = Vector3.MoveTowards(transform.position, target[current].position, speed * Time.deltaTime);
            GetComponent<Rigidbody>().MovePosition(pos);
              
        } else current = (current + 1) % target.Length;      


IEnumerator Example()
   {
     
       yield return new [URL='https://docs.unity3d.com/ScriptReference/WaitForSeconds.html']WaitForSeconds[/URL](5);
    
   }
} 
}

Thank you for your help

Sorry for any typo’s coded on the fly. didn’t test

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class movingCubus : MonoBehaviour {
    public Transform[] target;
    public float speed;
    public int current;
    private bool isWaiting = false;
    void Start(){
    
        StartCoroutine(Example());
    }
  
    // Update is called once per frame
    void Update ()
    {
        if(!isWaiting) //check if waiting == false
        {
            if (transform.position != target[current].position)
            {
                Vector3 pos = Vector3.MoveTowards(transform.position, target[current].position, speed * Time.deltaTime);
                GetComponent<Rigidbody>().MovePosition(pos);
            }else
            {
                current = (current + 1) % target.Length; 
                StartCoroutine(Wait()); // start the coroutine to wait
            }
        }
    }
    IEnumerator Wait()
    {   
        isWaiting = true;  //set the bool to stop moving
        print("Start to wait");
        yield return new WaitForSeconds(5); // wait for 5 sec
        print("Wait complete");
        isWaiting = false; // set the bool to start moving
       
    }
   
    IEnumerator Example()
   {
    
       yield return new [URL='https://docs.unity3d.com/ScriptReference/WaitForSeconds.html']WaitForSeconds[/URL](5);
  
   }
}
2 Likes

You are a god. Thank you :slight_smile:

Oh btw, I’m trying to make it so it only waits when current == 3, should I make a new function or add another else statement?

Thanks!

Just put an if around the StartCoroutine:

if(current == 3)
{
    StartCoroutine(Wait());
}
current = (current + 1) % target.Length;

If you want it to wait when looping back to the first point, regardless of the count, then you could instead do:

current = (current + 1) % target.Length;
if(current == 0)
{
    StartCoroutine(Wait());
}
1 Like

Thank you so much!!! <3