Colliders, NavMesh and Agents

Modified on Mon, 04 May 2020 at 03:28 PM

VoxelPlay supports collider and NavMesh automatic generation.


Colliders


Collider meshes are generated automatically for all voxel definitions with RenderType = Opaque. Vegetation and water voxels are always excluded. Collider meshes are generated using a greedy mesh algorithm which minimizes vertex count. Collider generation can be disabled in Voxel Play Environment inspector.


NavMesh


Similarly to the colliders, if this option is enabled in Voxel Play Environment inspector, Voxel Play will generate NavMesh meshes so agents can be created and navigate through the scene using Unity standard AI APIs. As with colliders, NavMeshes are generated using a greedy mesh algorithm. Only voxels flagged with "Navigatable" property will be considered for inclusion in the NavMesh.


Before creating agents, make sure the NavMesh at that position is ready to be used by Unity. Call ChunkHasNavMeshReady(chunk) method to validate the position before spawning any character that may use the NavMesh system.


Refining NavMesh generation


By default, Voxel Play sets NavMesh build settings properties like agentClimb to 1m and agentSlope to 60 degrees (these values are hardcoded in VoxelPlayEnvironment.NavMesh.cs and matches Voxel Play cubic world design).


If you need to adjust the precision of the generated NavMesh, you can set different values for tileSize and voxelSize using this code in the InitNavMesh() method:

navMeshBuildSettings.tileSize = newValue (default: 256)
navMeshBuildSettings.overrideTileSize = true;
navMeshBuildSettings.voxelSize = newValue (default: 0,1666)
navMeshBuildSettings.overrideVoxelSize = true;


Please read Unity documentation for more details.



Agents


The following code from demo scene 3 waits until the chunk at a given position is created and its NavMesh has been generated:


IEnumerator CreateFoes() {
            WaitForSeconds waitOneSecond = new WaitForSeconds (1f);

            // Create foes that follows player
            for (int k = 0; k < 3; k++) {
                Vector3 foeInitialPosition = new Vector3 (Random.value * 50 - 25, 51f, 70);
                VoxelChunk chunk = null;
                while (chunk == null || !env.ChunkHasNavMeshReady(chunk)) {
                    env.GetChunk (foeInitialPosition, out chunk, true);
                    yield return waitOneSecond;
                }
                GameObject foe = GameObject.CreatePrimitive (PrimitiveType.Cylinder);
                foe.transform.position = foeInitialPosition;
                foe.GetComponent<Renderer> ().material.color = Color.black;
                foe.AddComponent<FoeController> ();
            }
}


When the character or foe is active, you can use the NavMeshAgent to move it to a target destination. Attach the following script to the foe gameobject to make it follow the player:


using UnityEngine;
using UnityEngine.AI;
using VoxelPlay;


public class FoeController : MonoBehaviour {

        NavMeshAgent agent;

        void Start () {
            agent = gameObject.AddComponent<NavMeshAgent> ();
        }


        // Update is called once per frame
        void Update () {
            if (Random.value > 0.99f) {
                agent.SetDestination (VoxelPlayEnvironment.instance.characterController.transform.position);
            }
        }
}

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select atleast one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article