Example #1
0
        // Update is called once per frame
        void Update()
        {
            //Remet le firstAgent a null pour éviter les erreurs une fois un firstAgent détruit
            NavMeshAgent firstAgent = null;

            //Récupère les ennemis qui entre en collision avec la range de la tourelle (2e enfant de la tourelle)
            Collider[] hitColliders = Physics.OverlapSphere(transform.position, (transform.localScale.x * transform.GetChild(1).localScale.x) / 2, ennemyLayer);
            //Debug.Log(hitColliders.Length);
            //Pour chaque ennemi récupéré
            foreach (var hitCollider in hitColliders)
            {
                //Récupère le NavMeshAgent pour connaitre sa progression
                var agent = hitCollider.GetComponent <NavMeshAgent>();
                //Si aucun ennemi n'est désigné comme firstAgent, définit le premier ennemi comme FirstAgent
                if (firstAgent == null)
                {
                    firstAgent = agent;
                }
                //Récupère le firstAgent le plus avancé
                else if (agent.GetPathRemainingDistance() < firstAgent.GetPathRemainingDistance())
                {
                    //L'ennemi actuel devient le firstAgent
                    firstAgent = agent;
                }
            }
            //Tire sur le FirstAgent
            Shoot(firstAgent);
        }
Example #2
0
    ///////////////////////////////////////////////////////////////////////////////
    /////////////////////////// Attack Section End ////////////////////////////////

    void Update()
    {
        moveRange    = agent.GetPathRemainingDistance();
        calMoveRange = thePath.GetPathRemainingDistance2();



        if (gm.selectedUnit == this)
        {
            //////////////////////////////////// Calculated Path //////////////////////////////////////

            if (agent.isStopped == true)
            {
                RaycastHit distance;
                Ray        ray2 = cam.ScreenPointToRay(Input.mousePosition);
                if (Physics.Raycast(ray2, out distance))
                {
                    worldPosition = distance.point;
                }

                NavMesh.CalculatePath(transform.position, worldPosition, NavMesh.AllAreas, thePath);

                for (int i = 0; i < thePath.corners.Length - 1; i++)
                {
                    Debug.DrawLine(thePath.corners[i], thePath.corners[i + 1], Color.red);
                }                                                                                                                               // Draws the calculate Path
            }

            //////////////////////////////////// SetPath //////////////////////////////////////////////

            textActionPoints.text = "Action Points: " + Mathf.Round(actionPoints * 10) / 10;

            //Debug.Log(actionPoints);
            if (agent.isStopped == true && calMoveRange <= actionPoints) //Pathline dosen't redraw until Unit arrives destination
            {
                Ray        ray = cam.ScreenPointToRay(Input.mousePosition);
                RaycastHit hit;

                if (Physics.Raycast(ray, out hit))
                {
                    agent.SetDestination(hit.point);
                }
                // Displaying the Path
                lineRendererPath.positionCount = agent.path.corners.Length;
                lineRendererPath.SetPositions(agent.path.corners);
                lineRendererPath.enabled = true;
            }
            else if (agent.isStopped == true && calMoveRange >= actionPoints)
            {
                agent.ResetPath(); // Fix for too much Movement range
                lineRendererPath.enabled = false;
            }

            if (actionPoints <= 1) // switches of the line renderer if dont habe AP anymore
            {
                lineRendererPath.enabled = false;
            }

            // If this timer goes up else happens
            if (selectionTimer >= 0)
            {
                agent.isStopped = true;
                selectionTimer -= Time.deltaTime;
            }
            else
            {
                if (Input.GetMouseButtonDown(0) && agent.isStopped == true && lineRendererPath.enabled == true)
                {
                    actionPoints    -= moveRange;
                    initiateMovement = true;
                    agent.isStopped  = false;
                }
                textMoveRange.text = "Move Range: " + Mathf.Round(moveRange * 10) / 10;
            }
        }
        else
        {
            ///////////////////////////////////////////////////////////////////////////////////////////
            ///////////////////////////// Resetting Area after deselection ////////////////////////////

            initiateMovement         = false;
            lineRendererPath.enabled = false;
            selectionTimer           = 0.2f;
        }

        //statement checks if we reached our destination
        if (agent.remainingDistance > agent.stoppingDistance)
        {
            character.Move(agent.desiredVelocity, false, false);
        }
        else
        {   //Stops player
            character.Move(Vector3.zero, false, false);
            agent.isStopped = true;
        }
    }