Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Problem with curved LineRenderer: a "parasite" line is being rendered

Discussion in 'Scripting' started by Deleted User, Mar 11, 2016.

  1. Deleted User

    Deleted User

    Guest

    Hi everybody,

    As an old-timer from xna, I am pretty new with components such as LineRenderer.
    So, after 24 hours of googling, I still haven't found the solution to my problem.

    I am making a 3d-shmup battle, Lylat Wars style. At one moment, I release some parabolic lasers locked on multiple targets, like in Panzer Dragoon.

    I acquire the positions of locked enemies in playerTargets (a List of class PlayerTargetLock, whatever).

    then I instantiate my shot(s):

    Code (CSharp):
    1. for (int i = 0; i < playerTargets.Count; i++)
    2.                 {
    3.                     Shot1 locShot1 = (Shot1)Instantiate(playerShot1,
    4.                                                         transform.position + transform.forward,
    5.                                                         transform.rotation);
    6.                     locShot1.SetTarget(playerTargets[i].transform.position, i * 45.0f);
    7.                 }
    In Shot1 class,

    Code (CSharp):
    1. public void SetTarget(Vector3 _target, float _angle)
    2.     {
    3.         transform.Rotate(_angle, 0, _angle);
    4.  
    5.         iniPoints = new List<Vector3>();
    6.         iniPoints.Add(transform.position);
    7.         iniPoints.Add(transform.position + transform.forward * 5);
    8.         iniPoints.Add(_target);
    9.  
    10.         SetCurvedPath(5.0f);
    11.  
    12.         lineRenderer = gameObject.GetComponent<LineRenderer>();
    13.         lineRenderer.transform.position = transform.position;
    14.         lineRenderer.SetVertexCount(curvedPoints.Count);
    15.         lineRenderer.SetPosition(0, transform.position);
    16.         lineRenderer.SetWidth(0.45f, 0.45f);      
    17.     }
    where I store 3 points: origin, intermediate (the top of the parabole) and target in iniPoints (List<Vector3>);
    SetCurvedPath is a beziers algo which generates a curvedPoints (also List<Vector3()) with the positions of the path of the laser;

    Then I render my LineRenderer from the list of points stored in this curvedPoints:

    Code (CSharp):
    1. void Update ()
    2.     {
    3.         if (curvedLineDrawer < curvedPoints.Count - 1)
    4.         {
    5.             curvedLineDrawer++;
    6.  
    7.             for (int i = 1; i < curvedLineDrawer; i++)
    8.             {
    9.                 lineRenderer.SetPosition(i, curvedPoints[i]);
    10.             }
    11.         }
    12.  
    13.         curvedLineTime++;
    14.         if (curvedLineTime > 100)
    15.         {
    16.             Destroy(gameObject);
    17.         }
    18.     }
    where curvedLineDrawer is a counter for gradually drawing the laser, curvedLineTime only a timer.

    My problem is the parabolic laser if rendering, but there is this second line which seems to be drawn from the origin of the map (?) and the latest position of my laser, which means curvedPoints[curvedLineDrawer].

    I tried to modify the vertexCount, work with less curved points, it doesn't fix it.

    I am certain I am missing something very simple here, but one again I am not familiar with LineRenderer (nor Unity).

    Thank you for your time,

    Cyril
     
  2. Deleted User

    Deleted User

    Guest

    A px of my problem: from the ship down the screento the targetted red... thing on the left, the parabolic laser.
    Coming from the right to the target, this bloody parasite line.
     

    Attached Files:

  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    sounds a lot like the last vertex point is (0,0,0)... easiest way to confirm that would be to make curvedPoints a public array you can see in the inspector.
     
  4. Deleted User

    Deleted User

    Guest

    Thank you.. I know,right? But no:
     

    Attached Files:

  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,735
    I believe you have extra points in your LineRenderer (because you set its vertex count to curvedPoints.Count), but your "curvedLineDrawer" never reaches that number (due to "if (curvedLineDrawer < curvedPoints.Count - 1)").

    So your curvedPoints array has 10 things, therefore your LineRenderer has 10 vertices, but curvedPointDrawer is anywhere from 0 to 8, and the last vertex of the LineRenderer is never set.
    I would set, within that if statement, set lineRenderer.SetVertexCount(curvedPointDrawer).
     
    Deleted User likes this.
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,370
    The .SetPosition function is zero-based, not 1-based. If you never set the zero element, it will be (0,0,0) and a line will be drawn at the start. Is that what you're seeing?
     
  7. Deleted User

    Deleted User

    Guest

    Hi, actually the set to 0 is in the SetTarget method, before the SetVertexCount, in order to spare an iteration in the loop of the Update. Even in the loop with i initialized at 0, it doesn't change anything.
     
  8. Deleted User

    Deleted User

    Guest

    ...and there we are! Yes, the SetVertexCount had to be updated according to curvedPointDrawer.
    Thank you so much, have a nice one.
     
    ericbegue likes this.