Jump to content

CharacterControllerWaitForTerrainLoad script for those of you who do not use a rigid body but need a wait for terrain load script


Cliff

Recommended Posts

I don't use a rigidbody for my character controller as I use Game Creator 2 and mostly use the character controller for my player. I was running into issues where I needed a script to wait for the terrain to load, but using a character controller.

I'll share the script with those of you who may find it useful so you don't have to reinvent the wheel. I would have loved to find this script instead of taking the time to do it myself.

It seems to work for me, and hopefully it will for you too.

 

---------------------------------------------------------------------------------

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Gaia
{
    /// <summary>
    /// Just a small helper script to wait until the terrain below a character controller is being loaded in before
    /// activating gravity on the character controller
    /// </summary>
    public class CharacterControllerWaitForTerrainLoad : MonoBehaviour
    {
        CharacterController m_watchedCharacterController;
        TerrainScene m_terrainScene;
        bool m_invokeStarted = false;

        /// <summary>
        /// Delay until the character controller is activated after the terrain is loaded.
        /// </summary>
        public float m_activateDelay = 2f;

        /// <summary>
        /// Add additional components here that need to be activated with the character controller
        /// </summary>
        public List<MonoBehaviour> m_componentsToActivate = new List<MonoBehaviour>();

        // Start is called before the first frame update
        void Start()
        {
            if (TerrainLoaderManager.TerrainScenes.Count > 0)
            {
                m_watchedCharacterController = GetComponent<CharacterController>();

                // Find the relevant scene
                foreach (TerrainScene p in TerrainLoaderManager.TerrainScenes)
                {
                    if (p.m_bounds.Contains(transform.position))
                    {
                        m_terrainScene = p;
                        // Disable character controller movement initially
                        m_watchedCharacterController.enabled = false;
                    }
                }

                // No terrain scene found? Nothing to do then.
                if (m_terrainScene == null)
                {
                    Destroy(this);
                }
            }
            else
            {
                Destroy(this);
            }
        }

        // Update is called once per frame
        void Update()
        {
            if (m_terrainScene != null && m_terrainScene.m_regularLoadState != LoadState.Loaded)
            {
                // Disable character controller movement while terrain is not loaded
                m_watchedCharacterController.enabled = false;
            }
            else
            {
                if (!m_invokeStarted)
                {
                    Invoke("ActivateCharacterController", m_activateDelay);
                }
            }
        }

        void ActivateCharacterController()
        {
            // Enable character controller movement
            m_watchedCharacterController.enabled = true;

            foreach (MonoBehaviour component in m_componentsToActivate)
            {
                component.enabled = true;
            }

            Destroy(this);
        }
    }
}
 

  • Thanks 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Tell a friend

    Love Canopy - Procedural Worlds? Tell a friend!
  • Need help?

    We work with some of the biggest brands in global gaming, automotive, technology, and government to create environments, games, simulations, and product launches for desktop, mobile, and VR.

    Our unique expertise and technology enable us to deliver solutions that look and run better at a fraction of the time and cost of a typical project.

    Check out some of our non-NDA work in the Gallery, and then Contact Us to accelerate your next project!

×
×
  • Create New...