I have a code that uses OnClick to send a navmesh agent to a pre-placed destination point. This is for a “virtual tour” of sorts. I also want a “Guided tour” routine that just cycles through all the destination points.
Code:
public class UIMotor : MonoBehaviour {
public Button m_dest1, m_dest2, m_guideTour;
private NavMeshAgent agent;
public Transform dest1, dest2;
public Transform[] waypoints;
public Vector3 destination;
// Use this for initialization
void Start () {
agent = GetComponent<NavMeshAgent>();
destination = agent.destination;
Button btn1 = m_dest1.GetComponent<Button>();
Button btn2 = m_dest2.GetComponent<Button>();
//Button btn3 = m_guideTour.GetComponent<Button>();
btn1.onClick.AddListener(GoToDest1);
btn2.onClick.AddListener(GoToDest2);
//btn3.onClick.AddListener(GuideTour);
}
// Update is called once per frame
void Update () {
Button btn3 = m_guideTour.GetComponent<Button>();
btn3.onClick.AddListener(GuideTour);
}
void GoToDest1()
{
agent.destination = dest1.position;
}
void GoToDest2()
{
agent.destination = dest2.position;
}
void GuideTour()
{
StartCoroutine(GuidedTour());
}
private bool AtPoint()
{
if(Vector3.Distance(destination, dest1.position) <= 0.5f )
{
return true;
}
else
{
return false;
}
}
IEnumerator GuidedTour()
{
agent.destination = dest1.position;
yield return new WaitUntil(AtPoint);
agent.destination = dest2.position;
yield return new WaitForSecondsRealtime(10);
agent.destination = dest1.position;
}
}
The coroutine works if I just do WaitForSecondsRealtime between each destination, but that’s not great since I don’t want to test each travel time by hand. With the code above it does go to destination 1, but just stays there. Did the same even with a simple destination == target.postion bool.
Is the code broken or am I now missing something to restart the code after the WaitUntil?