Search Unity

Problem updating line renderer's start and end points positions at runtime

Discussion in 'General Graphics' started by rodolpheg, Mar 23, 2017.

  1. rodolpheg

    rodolpheg

    Joined:
    Oct 17, 2016
    Posts:
    4
    I'm trying to connect two GameObject (sphereObject0 and sphereObject2) with a line renderer lineRenderer0_2.

    It shows up fine at first. My problem is that I need the line renderer to update its start and end positions to match the positions of sphereObject0 and sphereObject2.

    For some reasons, my line renderer is absolutely static, and I can't find the way to make it adjust its start and end positions together with the spheres.

    Here's my C# script, any idea about what I am doing wrong? Any help is greatly appreciated!

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class autoCreateNetwork : MonoBehaviour {
    5.  
    6.     public bool sphereHasGravity = false;
    7.     private Collider coll;
    8.     private Renderer rend;
    9.     private SpringJoint springJoint;
    10.     public Material lines;
    11.  
    12.     GameObject sphereObject0;
    13.     GameObject sphereObject2;
    14.     LineRenderer lineRenderer0_2 ;
    15.  
    16.     void Start () {
    17.  
    18.         // Spawn a sphere and set it up
    19.         sphereObject0 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
    20.         sphereObject0.transform.position = new Vector3(-10.2571400951f, 98.8977277884f, -19.9870244083f);
    21.         Rigidbody sphereBody0 = sphereObject0.AddComponent<Rigidbody>();
    22.         sphereBody0.mass = 1f;
    23.         // Physics model
    24.         coll = sphereBody0.GetComponent<Collider> ();
    25.         PhysicMaterial material0 = new PhysicMaterial();
    26.         material0.dynamicFriction = 1;
    27.         coll.material = material0;
    28.         coll.material.bounciness = .8f;
    29.         // Render (color, smoothness, etc.)
    30.         rend = sphereBody0.GetComponent<Renderer>();
    31.         rend.material.color = new Color32(171,166,164,255);
    32.         rend.material.SetFloat("_Metallic", 0.0f);
    33.         rend.material.SetFloat("_Glossiness", 0.0f);
    34.         sphereBody0.useGravity = sphereHasGravity;
    35.         sphereBody0.transform.localScale = new Vector3(0.5f,0.5f,0.5f);
    36.  
    37.         // Spawn a sphere and set it up
    38.         sphereObject2 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
    39.         sphereObject2.transform.position = new Vector3(-28.1943570987f, 89.5665878403f, 5.43686642264f);
    40.         Rigidbody sphereBody2 = sphereObject2.AddComponent<Rigidbody>();
    41.         sphereBody2.mass = 1f;
    42.         // Physics model
    43.         coll = sphereBody2.GetComponent<Collider> ();
    44.         PhysicMaterial material2 = new PhysicMaterial();
    45.         material2.dynamicFriction = 1;
    46.         coll.material = material2;
    47.         coll.material.bounciness = .8f;
    48.         // Render (color, smoothness, etc.)
    49.         rend = sphereBody2.GetComponent<Renderer>();
    50.         rend.material.color = new Color32(105,150,187,255);
    51.         rend.material.SetFloat("_Metallic", 0.0f);
    52.         rend.material.SetFloat("_Glossiness", 0.0f);
    53.         sphereBody2.useGravity = sphereHasGravity;
    54.         sphereBody2.transform.localScale = new Vector3(0.5f,0.5f,0.5f);
    55.  
    56.         // Spawn a line and set it up
    57.         GameObject line0_2 = new GameObject();
    58.         LineRenderer lineRenderer0_2 = line0_2.AddComponent<LineRenderer>();
    59.         lineRenderer0_2.material = lines;
    60.         lineRenderer0_2.widthMultiplier = 0.0482773661501f;
    61.         lineRenderer0_2.SetPosition(0, sphereObject0.transform.position);
    62.         lineRenderer0_2.SetPosition(1, sphereObject2.transform.position);
    63.         float alpha0_2 = .9f;
    64.         Gradient gradient0_2 = new Gradient();
    65.         gradient0_2.SetKeys(
    66.             new GradientColorKey[] { new GradientColorKey(new Color32(171,166,164,255), 0.0f), new GradientColorKey(new Color32(105,150,187,255), 1.0f) },
    67.             new GradientAlphaKey[] { new GradientAlphaKey(alpha0_2, 0.0f), new GradientAlphaKey(alpha0_2, 1.0f) }
    68.         );
    69.         lineRenderer0_2.colorGradient = gradient0_2;
    70.         lineRenderer0_2.useWorldSpace = true;
    71.  
    72.     }
    73.  
    74.     void Update () {
    75.  
    76.             lineRenderer0_2.SetPosition(0, sphereObject0.transform.position);
    77.             lineRenderer0_2.SetPosition(1, sphereObject2.transform.position);
    78.  
    79.         }
    80.     }