0
\$\begingroup\$
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class ShowMeshBounds : MonoBehaviour
{
    public GameObject[] objectsToDraw;
    public GameObject prefabEffect;
    public Color color = Color.green;

    private Vector3 v3FrontTopLeft;
    private Vector3 v3FrontTopRight;
    private Vector3 v3FrontBottomLeft;
    private Vector3 v3FrontBottomRight;
    private Vector3 v3BackTopLeft;
    private Vector3 v3BackTopRight;
    private Vector3 v3BackBottomLeft;
    private Vector3 v3BackBottomRight;

    private float counter = 0;
    public bool animateLines;
    public float speed = 1f;

    private List<GameObject> allLines = new List<GameObject>();
    private List<GameObject> instancesToMove = new List<GameObject>();
    private List<Vector3> allPoints = new List<Vector3>();
    private Vector3 startPos;
    private Vector3 endPos;
    private LineRenderer lr;

    private void Start()
    {
        DrawObjects();
    }

    private void DrawObjects()
    {
        objectsToDraw = GameObject.FindGameObjectsWithTag("SpawnObject");

        for (int i = 0; i < objectsToDraw.Length; i++)
        {
            CalcPositons(objectsToDraw[i]);
            DrawBox();

            for (int x = 0; x < allLines.Count; x++)
            {

                allLines[x].transform.parent = objectsToDraw[i].transform;
            }
        }

        DuplicatePrefabEffects(1);

        moveLine();
        //StartCoroutine(moveLines());
    }

    private void DuplicatePrefabEffects(int duplicationNumber)
    {
        for (int i = 0; i < duplicationNumber; i++)
        {
            var go = Instantiate(prefabEffect);
            go.tag = "Duplicated Prefab";
            go.name = "Duplicated Prefab";
            instancesToMove.Add(go);
        }
    }

    void CalcPositons(GameObject objectToDrawTransform)
    {
        Bounds bounds = objectToDrawTransform.GetComponent<MeshFilter>().sharedMesh.bounds;

        Vector3 v3Center = bounds.center;
        Vector3 v3Extents = bounds.extents;

        v3FrontTopLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z);  // Front top left corner
        v3FrontTopRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z);  // Front top right corner
        v3FrontBottomLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z);  // Front bottom left corner
        v3FrontBottomRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z);  // Front bottom right corner
        v3BackTopLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z);  // Back top left corner
        v3BackTopRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z);  // Back top right corner
        v3BackBottomLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z);  // Back bottom left corner
        v3BackBottomRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z);  // Back bottom right corner

        v3FrontTopLeft = objectToDrawTransform.transform.TransformPoint(v3FrontTopLeft);
        v3FrontTopRight = objectToDrawTransform.transform.TransformPoint(v3FrontTopRight);
        v3FrontBottomLeft = objectToDrawTransform.transform.TransformPoint(v3FrontBottomLeft);
        v3FrontBottomRight = objectToDrawTransform.transform.TransformPoint(v3FrontBottomRight);
        v3BackTopLeft = objectToDrawTransform.transform.TransformPoint(v3BackTopLeft);
        v3BackTopRight = objectToDrawTransform.transform.TransformPoint(v3BackTopRight);
        v3BackBottomLeft = objectToDrawTransform.transform.TransformPoint(v3BackBottomLeft);
        v3BackBottomRight = objectToDrawTransform.transform.TransformPoint(v3BackBottomRight);
    }

    void DrawBox()
    {
        SpawnLineGenerator(v3FrontTopLeft, v3FrontTopRight, color);
        SpawnLineGenerator(v3FrontTopRight, v3FrontBottomRight, color);
        SpawnLineGenerator(v3FrontBottomRight, v3FrontBottomLeft, color);
        SpawnLineGenerator(v3FrontBottomLeft, v3FrontTopLeft, color);

        SpawnLineGenerator(v3BackTopLeft, v3BackTopRight, color);
        SpawnLineGenerator(v3BackTopRight, v3BackBottomRight, color);
        SpawnLineGenerator(v3BackBottomRight, v3BackBottomLeft, color);
        SpawnLineGenerator(v3BackBottomLeft, v3BackTopLeft, color);

        SpawnLineGenerator(v3FrontTopLeft, v3BackTopLeft, color);
        SpawnLineGenerator(v3FrontTopRight, v3BackTopRight, color);
        SpawnLineGenerator(v3FrontBottomRight, v3BackBottomRight, color);
        SpawnLineGenerator(v3FrontBottomLeft, v3BackBottomLeft, color);

        // Without color
        SpawnLineGenerator(v3FrontTopLeft, v3FrontTopRight);
        SpawnLineGenerator(v3FrontTopRight, v3FrontBottomRight);
        SpawnLineGenerator(v3FrontBottomRight, v3FrontBottomLeft);
        SpawnLineGenerator(v3FrontBottomLeft, v3FrontTopLeft);

        SpawnLineGenerator(v3BackTopLeft, v3BackTopRight);
        SpawnLineGenerator(v3BackTopRight, v3BackBottomRight);
        SpawnLineGenerator(v3BackBottomRight, v3BackBottomLeft);
        SpawnLineGenerator(v3BackBottomLeft, v3BackTopLeft);

        SpawnLineGenerator(v3FrontTopLeft, v3BackTopLeft);
        SpawnLineGenerator(v3FrontTopRight, v3BackTopRight);
        SpawnLineGenerator(v3FrontBottomRight, v3BackBottomRight);
        SpawnLineGenerator(v3FrontBottomLeft, v3BackBottomLeft);
    }

    void SpawnLineGenerator(Vector3 start, Vector3 end)
    {
        allPoints.Add(start);
        allPoints.Add(end);
    }

    void SpawnLineGenerator(Vector3 start, Vector3 end, Color color)
    {
        GameObject myLine = new GameObject();

        myLine.tag = "FrameLine";
        myLine.name = "FrameLine";

        myLine.AddComponent<LineRenderer>();
        myLine.AddComponent<EndHolder>();
        myLine.GetComponent<EndHolder>().EndVector = end;
        LineRenderer lr = myLine.GetComponent<LineRenderer>();
        lr.material = new Material(Shader.Find("Particles/Alpha Blended Premultiply"));
        lr.startColor = color;
        lr.useWorldSpace = false;
        lr.endColor = color;
        lr.startWidth = 0.1f;//0.03f;
        lr.endWidth = 0.1f;//0.03f;
        lr.numCapVertices = 5;
        lr.SetPosition(0, start);
        lr.SetPosition(1, start);

        allLines.Add(myLine);
    }

    IEnumerator moveLines()
    {
        for (int i = 0; i < allLines.Count; i++)
        {
            counter = 0;
            endPos = allLines[i].GetComponent<EndHolder>().EndVector;

            var dist = Vector3.Distance(endPos, startPos);
            while (counter < 25)
            {
                dist = Vector3.Distance(instancesToMove[0].transform.position, endPos);
                counter++;
                endPos = allLines[i].GetComponent<EndHolder>().EndVector;
                startPos = allLines[i].GetComponent<LineRenderer>().GetPosition(0);

                instancesToMove[0].transform.position =
                    Vector3.MoveTowards(startPos, endPos, counter / 25f * speed);

                allLines[i].GetComponent<LineRenderer>().SetPosition(1, instancesToMove[0].transform.position);//tempPos);

                yield return null;
            }
        }
    }

    IEnumerator moveLine()
    {
        yield return null;
    }
}

With the method moveLines I can animate 24 lines and create a 3d box/cube.

Now I want to do the same but with one line that will move between set of positions.

Instead adding a line to each start and end then adding one line to all the starts and ends using the List: allPoints and then using LineRenderer setpositons to draw and move the line between the points.

In the method void SpawnLineGenerator(Vector3 start, Vector3 end) I'm adding the start and end to the List and then I want to animate one line between the allPoints in the method moveLine

\$\endgroup\$
  • \$\begingroup\$ You can't draw a cube with a single continuous line without re-crossing the same edge twice - there are too many 3-way junctions. Do you have a particular order in which you want to see the 12 segments of the cube frame appear? \$\endgroup\$ – DMGregory Mar 26 '18 at 21:31
  • \$\begingroup\$ @DMGregory Yes the problem when drawing with the lines it's the order of the drawing. The drawing it self is fine but it's first drawing 4 lines then it's jumping and the drawing another 4 lines on the other side and what I want is that it will not jump but will move on one line to the other side and draw the next 4 or to draw the next 4 lines that are next the first. To make it looks like it's one line. This screenshot show what I mean by jumping: imgur.com/a/A1nr2 \$\endgroup\$ – Daniel Lip Mar 26 '18 at 21:42
  • \$\begingroup\$ @DMGregory What I want is to make it looks like one line or continues and not like now that it's changing position and jump to the other side and in the end it's connecting between them. For example draw the 4 right lines the the front or back lines then the left then the front/back. \$\endgroup\$ – Daniel Lip Mar 26 '18 at 21:43
  • \$\begingroup\$ Try drawing a simple storyboard of the sequence you want to see, and embed it as part of your question. \$\endgroup\$ – DMGregory Mar 26 '18 at 22:01
  • \$\begingroup\$ @DMGregory I did it by changing the order on my own and there is no way to do it without repeating at least 3 times on the same line. I just wonder if this kind of behaviour or order is possible to be make automatic ? I created 3 methods Front, Back, LeftRight: pastebin.com/MKEQNV1B This is what I want it move like but I wonder what will happen if the shape is not a cube/box for example a sphere or quad I will have to change the order each time ? \$\endgroup\$ – Daniel Lip Mar 27 '18 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.