News Forums RAIN General Discussion and Troubleshooting Adding NavMesh on Procedurally Generated Level on Runtime

Tagged: 

This topic contains 2 replies, has 2 voices, and was last updated by  EmpI 2 months, 2 weeks ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #40415

    EmpI
    Participant

    As it says on the tin.

    I’m looking for solutions for my game which has levels that are all procedurally generated on Runtime. Every time “play” is clicked in the Editor, a new level is created. I’d like for every time I click “play” for a navmesh to be built around the level, with holes cut inside the walls.

    I would also like for enemies which I already have randomly spawning in the level to move about and wander randomly (the idea I have is to randomly set their destination to random tiles on the map, and every time the enemy reaches that destination, the destination jumps to another random spot.)

    The second paragraph i’m confident I can handle on my own with simple C# programming, but for the first paragraph, I wouldn’t know where to begin. So help?

    #40420

    blockcipher
    Participant

    Here you go

    http://rivaltheory.com/forums/topic/using-rain-with-terrain-generated-at-runtime/

    and here is the code I’ve been using

    using System;
    using UnityEngine;
    using System.Collections;
    using RAIN.Navigation.NavMesh;
    public class NavMeshBuilder : MonoBehaviour
    {
        private NavMeshRig _navMeshRig;
        [SerializeField]
        private int _threadCount = 4;
        private int _timeToCreate;
        public static NavMeshBuilder Instance
        {
            get { return _instance; }
        }
        public delegate void NavMeshBuilt();
        public event NavMeshBuilt OnNavMeshBuilt;
        private static NavMeshBuilder _instance;
        private void Awake()
        {
            if (_instance != null)
            {
                DestroyImmediate(gameObject);
                return;
            }
            _instance = this;
        }
        public void GenerateNavMesh(NavMeshRig pNavMeshRig)
        {
            _navMeshRig = pNavMeshRig;
            StartCoroutine("GenNavMesh");
        }
        IEnumerator GenNavMesh()
        {
            _navMeshRig.NavMesh.UnregisterNavigationGraph();
            float m_startTime = Time.time;
            _navMeshRig.NavMesh.StartCreatingContours(_threadCount);
            while (_navMeshRig.NavMesh.Creating)
            {
                _navMeshRig.NavMesh.CreateContours();
                _timeToCreate++;
                Debug.Log(string.Format("creating nav mesh..{0}", _timeToCreate));
                yield return new WaitForSeconds(1);
            }
            if (OnNavMeshBuilt != null)
                OnNavMeshBuilt();
            float m_endTime = Time.time;
            Debug.Log(string.Format("NavMesh built in {0}s", (m_endTime - m_startTime)));
            _navMeshRig.NavMesh.RegisterNavigationGraph();
            _navMeshRig.Awake();
        }
        public bool IsNavMeshCreating()
        {
            return _navMeshRig.NavMesh.Creating;
        }
    }
    • This reply was modified 2 months, 2 weeks ago by  blockcipher.
    • This reply was modified 2 months, 2 weeks ago by  blockcipher.
    #40426

    EmpI
    Participant

    I got it to work! Excellent.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.