Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class RandomMovement : MonoBehaviour {
- public float range = .1f;
- float timer = 0;
- float reset = 0f;
- NavMeshAgent nav;
- Vector3 point;
- bool RandomPoint(Vector3 center, float range, out Vector3 result) {
- Vector3 randomPoint = center + Random.insideUnitSphere * range;
- NavMeshHit hit;
- if (NavMesh.SamplePosition(randomPoint, out hit, 1.0f, NavMesh.AllAreas))
- {
- result = hit.position;
- return true;
- }
- result = Vector3.zero;
- return false;
- }
- void Start()
- {
- nav = GetComponent<NavMeshAgent> ();
- }
- void Update()
- {
- reset += Time.deltaTime;
- timer += Time.deltaTime;
- if (timer >= 2 )
- {
- Move();
- if( nav.hasPath == true)
- {
- nav.Stop();
- if(reset >= 5)
- {
- reset = 0f;
- nav.ResetPath();
- }
- }
- }
- }
- void Move()
- {
- if(RandomPoint(transform.position, range, out point))
- {
- nav.SetDestination(point);
- Debug.Log(nav.hasPath);
- timer = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement