Example #1
0
    // Creates lines of prefabricated objects to show path character is going to take
    // Prefabs destroy themselves when character get near enough
    private void DrawPath()
    {
        Vector3[] posOnPath = new Vector3[m_Path.Count + 1];                 // Create a new array of positions the size of the path plus one for character
        posOnPath[0] = m_Character.transform.position;                       // Set first position to character position
        float dist = 0.0f;                                                   // Setup a total distance of path variable
        int   i    = 1;                                                      // Setup a counter for indexing

        foreach (Path_Node node in m_Path)                                   // Loop through all path nodes
        {
            posOnPath[i] = node.transform.position;                          // Add the nodes position to the array
            dist        += Vector3.Distance(posOnPath[i - 1], posOnPath[i]); // Add onto the total distance of path
            i++;                                                             // Increment counter
        }
        float distAlongPath = 0.0f;

        for (int k = 0; k < posOnPath.Length - 1; k++)
        {
            Vector3 node1         = posOnPath[k];
            Vector3 node2         = posOnPath[k + 1];
            int     amountToSpawn = Mathf.CeilToInt(Vector3.Distance(node1, node2) / 5.0f) - 1; // Amount needed based on spacing 5.0f, amount-=1 to prevent overshooting
            Vector3 dirSpawn      = Vector3.Normalize(node2 - node1);                           // Normalise vector between points to length 1
            Vector3 previousPoint = posOnPath[k];
            // For loop since dynamic amount
            for (int j = 0; j <= amountToSpawn; j++)
            {
                Vector3 SpawnPos = (node1 + (dirSpawn * (j * 5.0f))) + (Vector3.up * 5.0f);                 // Calculate a spawn position using first points location, the normalised vector, and set it's y to 5.0f
                distAlongPath += Vector3.Distance(previousPoint, SpawnPos);                                 // Increment distance along path variable
                GameObject        newPoint = Instantiate(m_VisualPF, SpawnPos, Quaternion.Euler(dirSpawn)); // Instantiate a new prefab with pointer to it, vector to set it's look rotation incase I change model to arrow, or make it move a certain way
                PathingVisualiser script   = newPoint.GetComponent <PathingVisualiser>();                   // Couldn't cast straight to it's script type when instantiating so get it here
                script.Character   = transform;                                                             // Pointer to character distance checking
                script.m_DebugMode = m_Character.GetDebugMode();                                            // Setting debug bool to be consistent with rest of game, this controls whether it's visible or not
                script.SetPathPercent(distAlongPath / dist);                                                // Set the visualisers colour using progress along path for it's lerp use
            }
        }
    }