Exemplo n.º 1
0
        // Use this for initialization
        void Start()
        {
            if (m_target == null)
            {
                return;
            }
            m_updateStopDistance       = false;
            m_targetPosition           = m_target.position;
            m_distanceToTarget         = (m_targetPosition - transform.position).magnitude;
            m_currentStopDistance      = UnityEngine.Random.Range(m_stopDistanceMin, m_stopDistanceMax);
            m_currentCollisionCount    = 0;
            m_currentCollisionHitArray = new RaycastHit[20]; //Change the size here if you want more collisions to be considered

            //Load up terrain trees if we are avoiding collisions
            if (m_avoidTreeCollisions)
            {
                m_terrainTreeManager = new TreeManager();
                m_terrainTreeManager.LoadTreesFromTerrain();
            }

            //Load up our own colliders - we will filter them out when checking for hits
            Collider[] colliders = GetComponentsInChildren <Collider>();
            foreach (Collider collider in colliders)
            {
                m_myColliders.Add(collider.GetInstanceID(), collider);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Fix up grounding and ground based rotation
        /// </summary>
        void LateUpdate()
        {
            if (m_target == null)
            {
                return;
            }

            if (m_groundToTerrain || m_rotateToTerrain)
            {
                Vector3 position = transform.position;
                Terrain terrain  = GetTerrain(position);
                if (terrain == null)
                {
                    return;
                }

                position.y = terrain.SampleHeight(position);

                if (m_groundToTerrain)
                {
                    transform.position = position;
                }

                if (m_rotateToTerrain)
                {
                    Vector3 terrainLocalPos = terrain.transform.InverseTransformPoint(position);
                    Vector3 normalizedPos   = new Vector3(Mathf.InverseLerp(0.0f, terrain.terrainData.size.x, terrainLocalPos.x),
                                                          Mathf.InverseLerp(0.0f, terrain.terrainData.size.y, terrainLocalPos.y),
                                                          Mathf.InverseLerp(0.0f, terrain.terrainData.size.z, terrainLocalPos.z));
                    Vector3 normal = terrain.terrainData.GetInterpolatedNormal(normalizedPos.x, normalizedPos.z);
                    //float steepness = terrain.terrainData.GetSteepness(normalizedPos.x, normalizedPos.z);
                    transform.rotation = Quaternion.FromToRotation(transform.up, normal) * transform.rotation;
                }
            }

            //Update collisions if seleted
            if (m_avoidObjectCollisions)
            {
                //Do sphere cast
                m_currentCollisionCount = Physics.SphereCastNonAlloc(transform.position, m_collisionRange, transform.forward, m_currentCollisionHitArray, 0f, m_objectCollisionLayer, QueryTriggerInteraction.UseGlobal);
            }

            if (m_avoidTreeCollisions)
            {
                //Load trees if not loaded
                if (m_terrainTreeManager == null)
                {
                    m_terrainTreeManager = new TreeManager();
                    m_terrainTreeManager.LoadTreesFromTerrain();
                }
                //Do tree check
                m_currentTreeCollisionCount = m_terrainTreeManager.GetTrees(transform.position, m_collisionRange + (m_collisionRange / 2f), ref m_currentTreeCollisionHitArray);
            }
        }