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