Search Unity

Why the allLines never get to the end ?

Discussion in 'Scripting' started by haimmoshe, Feb 16, 2018.

  1. haimmoshe

    haimmoshe

    Joined:
    Jun 3, 2017
    Posts:
    237
    The instancestoMove are getting to the end position but the allLines are not.

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Linq;
    5. using UnityEngine;
    6.  
    7. public class ShowMeshBounds : MonoBehaviour
    8. {
    9.    public GameObject prefabEffect;
    10.    public Color color = Color.green;
    11.  
    12.    private Vector3 v3FrontTopLeft;
    13.    private Vector3 v3FrontTopRight;
    14.    private Vector3 v3FrontBottomLeft;
    15.    private Vector3 v3FrontBottomRight;
    16.    private Vector3 v3BackTopLeft;
    17.    private Vector3 v3BackTopRight;
    18.    private Vector3 v3BackBottomLeft;
    19.    private Vector3 v3BackBottomRight;
    20.  
    21.    private float counter = 0;
    22.    public bool animateLines;
    23.    public float speed = 1f;
    24.  
    25.    private List<GameObject> allLines = new List<GameObject>();
    26.    private List<GameObject> instancesToMove = new List<GameObject>();
    27.    private Vector3 endPos;
    28.  
    29.    private void Start()
    30.    {
    31.        CalcPositons();
    32.        DrawBox();
    33.        allLines = GameObject.FindGameObjectsWithTag("FrameLine").ToList();
    34.  
    35.        DuplicatePrefabEffects(allLines.Count);
    36.        instancesToMove = GameObject.FindGameObjectsWithTag("Duplicated Prefab").ToList();
    37.  
    38.        StartCoroutine(moveStuff());
    39.    }
    40.  
    41.    private void DuplicatePrefabEffects(int duplicationNumber)
    42.    {
    43.        for (int i = 0; i < duplicationNumber; i++)
    44.        {
    45.            var go = Instantiate(prefabEffect);
    46.            go.tag = "Duplicated Prefab";
    47.            go.name = "Duplicated Prefab";
    48.        }
    49.    }
    50.  
    51.    void CalcPositons()
    52.    {
    53.        Bounds bounds = GetComponent<MeshFilter>().sharedMesh.bounds;
    54.  
    55.        Vector3 v3Center = bounds.center;
    56.        Vector3 v3Extents = bounds.extents;
    57.  
    58.        v3FrontTopLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z);  // Front top left corner
    59.        v3FrontTopRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z);  // Front top right corner
    60.        v3FrontBottomLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z);  // Front bottom left corner
    61.        v3FrontBottomRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z);  // Front bottom right corner
    62.        v3BackTopLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z);  // Back top left corner
    63.        v3BackTopRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z);  // Back top right corner
    64.        v3BackBottomLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z);  // Back bottom left corner
    65.        v3BackBottomRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z);  // Back bottom right corner
    66.  
    67.        v3FrontTopLeft = transform.TransformPoint(v3FrontTopLeft);
    68.        v3FrontTopRight = transform.TransformPoint(v3FrontTopRight);
    69.        v3FrontBottomLeft = transform.TransformPoint(v3FrontBottomLeft);
    70.        v3FrontBottomRight = transform.TransformPoint(v3FrontBottomRight);
    71.        v3BackTopLeft = transform.TransformPoint(v3BackTopLeft);
    72.        v3BackTopRight = transform.TransformPoint(v3BackTopRight);
    73.        v3BackBottomLeft = transform.TransformPoint(v3BackBottomLeft);
    74.        v3BackBottomRight = transform.TransformPoint(v3BackBottomRight);
    75.    }
    76.  
    77.    void DrawBox()
    78.    {
    79.        SpawnLineGenerator(v3FrontTopLeft, v3FrontTopRight, color);
    80.        SpawnLineGenerator(v3FrontTopRight, v3FrontBottomRight, color);
    81.        SpawnLineGenerator(v3FrontBottomRight, v3FrontBottomLeft, color);
    82.        SpawnLineGenerator(v3FrontBottomLeft, v3FrontTopLeft, color);
    83.  
    84.        SpawnLineGenerator(v3BackTopLeft, v3BackTopRight, color);
    85.        SpawnLineGenerator(v3BackTopRight, v3BackBottomRight, color);
    86.        SpawnLineGenerator(v3BackBottomRight, v3BackBottomLeft, color);
    87.        SpawnLineGenerator(v3BackBottomLeft, v3BackTopLeft, color);
    88.  
    89.        SpawnLineGenerator(v3FrontTopLeft, v3BackTopLeft, color);
    90.        SpawnLineGenerator(v3FrontTopRight, v3BackTopRight, color);
    91.        SpawnLineGenerator(v3FrontBottomRight, v3BackBottomRight, color);
    92.        SpawnLineGenerator(v3FrontBottomLeft, v3BackBottomLeft, color);
    93.    }
    94.  
    95.    void SpawnLineGenerator(Vector3 start, Vector3 end, Color color)
    96.    {
    97.        GameObject myLine = new GameObject();
    98.  
    99.        myLine.tag = "FrameLine";
    100.        myLine.name = "FrameLine";
    101.  
    102.        myLine.AddComponent<LineRenderer>();
    103.        myLine.AddComponent<EndHolder>();
    104.        myLine.GetComponent<EndHolder>().EndVector = end;
    105.        LineRenderer lr = myLine.GetComponent<LineRenderer>();
    106.        lr.material = new Material(Shader.Find("Particles/Alpha Blended Premultiply"));
    107.        lr.startColor = color;
    108.        lr.useWorldSpace = false;
    109.        lr.endColor = color;
    110.        lr.startWidth = 0.1f;//0.03f;
    111.        lr.endWidth = 0.1f;//0.03f;
    112.        lr.SetPosition(0, start);
    113.        lr.SetPosition(1, start);
    114.    }
    115.  
    116.    IEnumerator moveStuff()
    117.    {
    118.        for (int i = 0; i < allLines.Count; i++)
    119.        {
    120.            counter = 0;
    121.  
    122.            while (Vector3.Distance(instancesToMove[i].transform.position, endPos) > 0.1f)
    123.            {
    124.                counter++;
    125.                endPos = allLines[i].GetComponent<EndHolder>().EndVector;
    126.                Vector3 startPos = allLines[i].GetComponent<LineRenderer>().GetPosition(0);
    127.                Vector3 tempPos = Vector3.Lerp(startPos, endPos, counter / 500f * speed);
    128.  
    129.                allLines[i].GetComponent<LineRenderer>().SetPosition(1, tempPos);
    130.  
    131.                instancesToMove[i].transform.position =
    132.                    Vector3.MoveTowards(startPos, endPos, counter / 25f * speed);
    133.  
    134.                //move towards destination
    135.                yield return null;
    136.            }
    137.        }
    138.    }
    139. }
    140.  
    I'm using StartCoroutine in the Start and then IEnumerator

    In this line I'm checking if the instancestoMove is getting to the end finished moving then move the next one.

    Code (csharp):
    1.  
    2. while (Vector3.Distance(instancesToMove[i].transform.position, endPos) > 0.1f)
    3.  
    If it's cube then I think that there are 8 connections on the out bounds and 12 lines. So there are 8 instancestoMove and 12 lines(LineRenderers).

    The result in the end:



    The lines in green are part of the allLines List The glowing ball objects are part of the instancesToMove

    But the lines never get to the end it's incomplete cube.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Debug your distance value and check if you are getting the value you expect.
     
    haimmoshe likes this.
  3. haimmoshe

    haimmoshe

    Joined:
    Jun 3, 2017
    Posts:
    237
    I checked the distance seems to be fine it's getting to 0.4 even less and when I'm looking on it in the editor I see that the balls the instancestoMove are moving to the end but the lines seems to be moving with the balls but the lines seems to be too short. The lines start moving from the balls start position same time as the balls and they stop when the balls stops.

    It seems like the lines are too short. Or maybe it's checking only the balls distance form the end maybe I should also check the lines distance from the end somehow ? Not sure why it happen.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Just thinking to myself .. wondering what does your program do (supposed to do?)

    I'm a little tired, but why does the move towards always use the startpos?
     
    haimmoshe likes this.
  5. haimmoshe

    haimmoshe

    Joined:
    Jun 3, 2017
    Posts:
    237
    Both balls and lines should start from the startPos to endPos. But the balls are just gameobjects when the lines are lineRenderer objects component.

    I tried now to change this line:

    Code (csharp):
    1.  
    2. Vector3 tempPos = Vector3.Lerp(startPos, instancesToMove[i].transform.position, counter / 500f * speed);
    3.  
    Instead endPos the line will move to the current ball position. But the result is like before. I thought if the line will move to the ball position all the time they will sync until the end but they are not.
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    That didn't fully answer my question, but I will just say what was on my mind anyways. Can you just parent one or the other and move 1 and not both?
     
    haimmoshe likes this.
  7. haimmoshe

    haimmoshe

    Joined:
    Jun 3, 2017
    Posts:
    237
    I tried but then remembered that the lines are objects with LineRenderer components each allLines GameObject have a LineRenderer component and what I'm moving is the line of the LineRenderer component not the gameobject the component is on.

    I guess I didn't exaplin what I mean by moving the lines:
    So if I will make allLines childs it will not move(draw/stretch) the lines.
    When I say move lines I mean like animating the lines not moving the gameobjects.

    For example like this: Look at second 9 about what I mean moving lines:

     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Right, of course.. And the connectors, those are supposed to be at the ends, yes?
     
    haimmoshe likes this.
  9. haimmoshe

    haimmoshe

    Joined:
    Jun 3, 2017
    Posts:
    237
    Yes, right.