Search Unity

Problem/Bug with Line Renderer?

Discussion in 'Scripting' started by Dawid_Matuszewski, Sep 20, 2017.

  1. Dawid_Matuszewski

    Dawid_Matuszewski

    Joined:
    Mar 2, 2016
    Posts:
    23
    Hello I'm a beginner when it comes to C# and Unity so my code may be imperfect. The script that I've got works perfectly fine except one thing. My aim is to write a tech tree to my game. On my scene I have:

    one gameobject which is the goal on the tech tree
    several gameobjects which are nodes on tech tree
    several gameobjects which are also nodes but they dont have sprites. They overlap with the gameobject from the subpoint above, they have the same size and position, we can call them ghosts

    When I use the start, the script draws the black line from nodes to the top of the tech tree correctly. Ghost components are turned off. When I click one of the nodes, the black line/path changes it's colour to green.This green line.path is also a part of the Ghost Components. When I click on it, I activate the Ghost Components. The thing is that when I click on the node, the green path, just for a split second, changes it position , it shifts more towards the centre of the node and then goes up to the top of the tech tree. However, when I start the program by activating the component in inspector instead of clicking on the node, the program draws the line correclty, without shifting its position. To sum up, when I start the program directly from the script, the green line has an error and it shifts towards the centre of the node before going upwards to the top of the tech tree. If I start the program from the inspector myself, it doesn't do the drill and no line is shifted before it goes up to the tech tree. Both gameobjects have the same script, but different colors in Component Line Renderer.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class DrawLine : MonoBehaviour {
    7.     public LineRenderer lineRenderer;
    8.     public float counter;
    9.     public float dist;
    10.  
    11.     public Transform origin;
    12.     public Transform destination;
    13.  
    14.     public MouseCollider ms;
    15.  
    16.     public GameObject dziecko;
    17.  
    18.     public float lineDrawSpeed;
    19.     public Vector3 pointAlongLine;
    20.     public Vector3 pointA;
    21.     public Vector3 pointB;
    22.     public float x;
    23.  
    24.     void Start () {
    25.  
    26.             lineDrawSpeed = .60f;
    27.             ms = this.gameObject.GetComponent<MouseCollider>();
    28.             origin.position = this.gameObject.GetComponent<Transform>().position;
    29.             destination.position = GameObject.FindGameObjectWithTag("przedmiot").GetComponent<Transform>().position;
    30.             lineRenderer = GetComponent<LineRenderer>();
    31.  
    32.             lineRenderer.SetPosition(0, origin.position);
    33.             lineRenderer.SetWidth(.45f, .45f);
    34.  
    35.             dist = Vector3.Distance(origin.position, destination.position);    
    36.      }
    37.  
    38.     void Update () {
    39.         if (counter <= dist)
    40.         {
    41.             counter = counter + lineDrawSpeed * Time.deltaTime;
    42.               x = Mathf.Lerp(0, dist, counter);
    43.              pointA = origin.position;
    44.              pointB = destination.position;
    45.  
    46.              pointAlongLine = x * Vector3.Normalize(pointB - pointA) + pointA;
    47.  
    48.             lineRenderer.SetPosition(1, pointAlongLine);
    49.         }
    50.  
    51.         if (ms.flagaOnClick == true) //if i click this gameobject then this two above components run
    52.         {
    53.             dziecko.GetComponent<LineRenderer>().enabled = true;
    54.             dziecko.GetComponent<DrawLine>().enabled = true;                          
    55.         }      
    56.       }
    57. }