uurha

Untitled

May 11th, 2022 (edited)
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. IEnumerator MoveRandomPos()
  2.     {
  3. while(true){
  4.        
  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.             {
  17.                 yield return StartCoroutine(MoveToPos(ray.direction));
  18.                 yield return new WaitForSeconds(Random.Range(4f, 7f));
  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. }
Add Comment
Please, Sign In to add comment