// 遊蕩 IEnumerator Wander() { // 隨機決定往左往右走 int direction = Random.Range(1, 3); bool point1 = true; switch (direction) { case 1: point1 = true; nav.MoveTo(pos1); break; case 2: point1 = false; nav.MoveTo(pos2); break; } while (true) { yield return(new WaitForSeconds(Time.deltaTime)); // 移動到點1,改移到點2 if (!nav.moving) { yield return(new WaitForSeconds(Random.Range(waitTime.x, waitTime.y))); point1 = !point1; nav.MoveTo(point1 ? pos1 : pos2); } } }