Exemple #1
0
    // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
    override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        ObstacleConstructor currentObstacle = m_listObstacleConstructors[m_ind];

        if (currentObstacle.GetCurrentState() == ObstacleConstructor.EState.Buildable)
        {
            NavMeshHit hit;
            NavMesh.SamplePosition(m_spawnunit.GetTarget().transform.position, out hit, Vector3.Distance(m_spawnunit.transform.position, m_spawnunit.GetTarget().transform.position) + 100, NavMesh.AllAreas);
            NavMeshAgent tempAgent = m_spawnunit.gameObject.GetComponent <NavMeshAgent>();
            if (tempAgent == null)
            {
                if (tempAgent == null)
                {
                    tempAgent = m_spawnunit.gameObject.AddComponent <NavMeshAgent>();
                }
            }
            tempAgent.CalculatePath(hit.position, m_newPath);

            //get intersection navmesh vs ckcollider
            Vector3[]  pathCorners = m_newPath.corners;
            RaycastHit colliderHit;
            //m_isCollider = (m_ind == m_listObstacleConstructors.Length - 1);

            for (int i = 0; i < pathCorners.Length - 1; i++)
            {
                Vector2 line    = new Vector2(pathCorners[i + 1].x - pathCorners[i].x, pathCorners[i + 1].z - pathCorners[i].z);
                Vector2 lineDir = line.normalized;
                Ray     lineRay = new Ray(pathCorners[i], lineDir);

                Debug.DrawLine(pathCorners[i], pathCorners[i + 1], Color.green, 1000, false);
                if (m_colliderOnAreaCollider.Raycast(lineRay, out colliderHit, Vector3.Distance(pathCorners[i], pathCorners[i + 1])))
                {
                    if (m_ind == 0)
                    {
                        animator.SetTrigger(Constant.BotTransition.s_return);
                    }
                    else
                    {
                        m_isCollider = true;
                        break;
                    }
                }
            }

            if (null != m_virtualWall)
            {
                Destroy(m_virtualWall);
            }

            if (m_isCollider)
            {
                Destroy(m_virtualArea);
                // Prepare pour Build Obstacles

                m_aiBuildObstacles.SetTypeBuild(ActiveObstacle.ObstacleType.SimpleWall);
                m_aiBuildObstacles.SetIndex(m_ind - 2);
                m_aiBuildObstacles.SetObstacleConstructorList(m_listObstacleConstructors);

                animator.SetTrigger(Constant.BotTransition.s_buildObstacle);
            }

            // construct virtual wall to calculate new path
            m_virtualWall = new GameObject();
            m_virtualWall.AddComponent <NavMeshObstacle>();
            m_virtualWall.GetComponent <NavMeshObstacle>().size = new Vector3(3, 3, 3);
            m_virtualWall.transform.position = currentObstacle.transform.position;
            m_virtualWall.GetComponent <NavMeshObstacle>().carving = true;
        }
        if (m_ind < m_listObstacleConstructors.Count - 1)
        {
            m_ind += 1;
        }
    }