SHOW:
|
|
- or go back to the newest paste.
| 1 | IEnumerator MoveRandomPos() | |
| 2 | {
| |
| 3 | - | yield return new WaitForSeconds(Random.Range(4f, 7f)); |
| 3 | + | while(true){
|
| 4 | ||
| 5 | - | StartCoroutine(MoveRandomPos()); |
| 5 | + | yield return null; |
| 6 | ||
| 7 | ||
| 8 | RaycastHit hit; | |
| 9 | Ray ray = new Ray(transform.position, Quaternion.Euler(0f, Random.Range(0f, 360f), 0f) * transform.forward); | |
| 10 | ||
| 11 | /*Debug.DrawRay(transform.position, ray.direction, Color.red, 3f);*/ | |
| 12 | ||
| 13 | if (Physics.Raycast(ray, out hit)) | |
| 14 | {
| |
| 15 | if (hit.distance > 2f) | |
| 16 | - | nav.SetDestination(ray.direction); |
| 16 | + | |
| 17 | yield return StartCoroutine(MoveToPos(ray.direction)); | |
| 18 | yield return new WaitForSeconds(Random.Range(4f, 7f)); | |
| 19 | - | } |
| 19 | + | |
| 20 | } | |
| 21 | } | |
| 22 | } | |
| 23 | ||
| 24 | IEnumerator MoveToPos(Vector3 direction){
| |
| 25 | nav.SetDestination(direction); | |
| 26 | var startPos = nav.transform.position; | |
| 27 | var randomDistance = Random.Range(4f, 7f); | |
| 28 | yield return new WaitUntil(()=> Vector3.Distance(startPos, nav.position) >= randomDistance); | |
| 29 | } |