Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.AI;
- using Random = UnityEngine.Random;
- public class GetNavmesh : MonoBehaviour
- {
- void DrawCircle(Vector3 center, float radius, Color color)
- {
- Vector3 prevPos = center + new Vector3(radius, 0, 0);
- for (int i = 0; i < 30; i++)
- {
- float angle = (float)(i + 1) / 30.0f * Mathf.PI * 2.0f;
- Vector3 newPos = center + new Vector3(Mathf.Cos(angle) * radius, 0, Mathf.Sin(angle) * radius);
- Debug.DrawLine(prevPos, newPos, color);
- prevPos = newPos;
- }
- }
- public float distanceToNewPoint = 25f; //how far away is the new point
- private NavMeshAgent agent;
- private void Start()
- {
- agent = GetComponent<NavMeshAgent>();
- StartCoroutine(FindNavmesh());
- }
- private IEnumerator FindNavmesh()
- {
- NavMeshPath path = new NavMeshPath();
- Vector3 newDest;
- do
- {
- //random vector3
- Vector3 randomDirection = new Vector3(Random.value, Random.value, Random.value);
- //set the magnitude of the vector3 (this could be randomized too if you want)
- randomDirection *= distanceToNewPoint;
- //randomly pick a negative value for the x or z
- if (Random.Range(0, 2) == 0) randomDirection.x *= -1;
- if (Random.Range(0, 2) == 0) randomDirection.z *= -1;
- //add the random vector to the current position
- Vector3 v = transform.position + randomDirection;
- //determine the y value by looking at the terrain height
- float y;
- y = Terrain.activeTerrain.SampleHeight(v) + Terrain.activeTerrain.transform.position.y;
- newDest = new Vector3(v.x, y, v.z);
- //calculate the path
- agent.CalculatePath(newDest, path);
- Debug.Log(path.status);
- yield return null;
- } while (path.status != NavMeshPathStatus.PathComplete);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement