Quantcast
Viewing latest article 2
Browse Latest Browse All 30

Best method to set a random position on NavMesh? (c#)

Hello! Some time ago, I wrote a little method that finds and returns a random point on a plane or terrain the renderer.bounds.size-way. I use it to randomize my navMeshAgents destinations, to make them move here and there for mecanim calibration purposes, but the problem with this method is, that it is cumbersome to set up non-quadratic (and soon randomly generated) areas, so, a way to return a random position directly from the navmesh itself would be a true relief! After a little research, I found this in the internet (unaltered copy): import UnityEngine class NavMeshVis (MonoBehaviour): [SerializeField] meshFilter as MeshFilter def Start (): verts as (Vector3) ids as (int) NavMesh.Triangulate(verts, ids) mesh = Mesh() mesh.vertices = verts mesh.triangles = ids meshFilter.mesh = mesh To see what's going on, I translated it to c#: using UnityEngine; public class NavMeshManager : MonoBehaviour { [SerializeField] MeshFilter meshFilter; void Start() { Vector3[] verts = NavMesh.CalculateTriangulation().vertices; int[] ids = NavMesh.CalculateTriangulation().indices; Mesh mesh = new Mesh(); mesh.vertices = verts; mesh.triangles = ids; meshFilter.mesh = mesh; } } I must admit, I don't have a clue on what this script does, but I guess it creates a mesh in the shape of the currently baked navmesh dimensions and makes it editable? And do I need to attach it to a gameobject? Or does anyone know a better way to get a random point on the navmesh during runtime? Any help is greatly appreciated! Greetings!

Viewing latest article 2
Browse Latest Browse All 30

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>