Advertisement
Guest User

Untitled

a guest
Jul 4th, 2015
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.05 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class RandomMovement : MonoBehaviour {
  5.     public float range = .1f;
  6.     float timer = 0;
  7.     float reset = 0f;
  8.     NavMeshAgent nav;
  9.     Vector3 point;
  10.  
  11.  
  12.  
  13.  
  14.     bool RandomPoint(Vector3 center, float range, out Vector3 result) {
  15.  
  16.             Vector3 randomPoint = center + Random.insideUnitSphere * range;
  17.             NavMeshHit hit;
  18.            
  19.             if (NavMesh.SamplePosition(randomPoint, out hit, 1.0f, NavMesh.AllAreas))
  20.         {
  21.                 result = hit.position;
  22.                 return true;
  23.  
  24.         }
  25.                 result = Vector3.zero;
  26.                 return false;
  27.     }
  28.     void Start()
  29.     {
  30.         nav = GetComponent<NavMeshAgent> ();
  31.  
  32.  
  33.     }
  34.    
  35.     void Update()
  36. {
  37.  
  38.    
  39.         reset += Time.deltaTime;
  40.         timer += Time.deltaTime;
  41.  
  42.         if (timer >= 2 )
  43.         {
  44.             Move();
  45.             if( nav.hasPath == true)
  46.         {
  47.                 nav.Stop();
  48.                 if(reset >= 5)
  49.             {
  50.                 reset = 0f;
  51.                 nav.ResetPath();
  52.             }
  53.         }
  54.     }
  55. }
  56.        
  57.  
  58.      void Move()
  59.     {
  60.             if(RandomPoint(transform.position, range, out point))
  61.  
  62.             {
  63.                 nav.SetDestination(point);
  64.                 Debug.Log(nav.hasPath);
  65.                 timer = 0;             
  66.             }
  67.  
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement