Search Unity

Dotted linerenderer + making lines disappear after time

Discussion in 'Scripting' started by Bogaland, Jul 12, 2018.

  1. Bogaland

    Bogaland

    Joined:
    Feb 19, 2017
    Posts:
    32
    Hello there!

    I have an object which is following a plotted direction created with my mouse. I'm using a linerenderer to show the future path of the object up until where my mouse has been. On default, I get a purple line just as the linerenderer does. However, I want it to be a dotted white line. I've Googled around and found some stuff but I can't really get it to work properly. For example, I've looked at this thread. It does not work properly unfortunately. I believe my problem is that my line is constantly increasing, since I get a very odd behavior of white lines stacking up and just looks terrible, see picture:



    The next issue can also be seen in the picture somewhat. I want the dots to disappear when the green-circled object has passed them. In the code I've attached below I thought this was done when the points get removed in linePoints.

    Note that the correctly shown dots at the bottom sometimes show up from where the green-circled object started, and not always. It is not a part of the path so to speak, but just something that's sometimes created when I deselect/reselect the object. It draws a new line from where my green-circled object spawned. I also wondering what might cause this? The white dots are from a material I made, just as in the thread I linked earlier.

    I will attach the code I think is relevant for this part. This is the entire class which controls the rotation of the green-circled object and the linerenderer.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class NewRotate : MonoBehaviour
    6. {
    7.  
    8.     public float speed = 0.01f;
    9.     public float rotationSpeed = 10f;
    10.     public float moveSpeed = 0.0001f;
    11.     public GameObject testPos;
    12.     public GameObject explosion;
    13.     public static bool spawned;
    14.     public bool selected;
    15.     private Plane ground;
    16.     private float numPoints = 50;
    17.     private bool launched = false;
    18.     private bool hasDetected = false;
    19.     private double dist;
    20.     private GameObject missileTarget;
    21.     private bool initiated;
    22.     private bool deselected = false;
    23.     public bool drawGizmos;
    24.     private int current;
    25.     public GameObject prefab;
    26.     public BoxCollider hitBox;
    27.     List<Vector3> linePoints = new List<Vector3>();
    28.     LineRenderer lineRenderer;
    29.     public float startWidth = 100.0f;
    30.     public float endWidth = 100.0f;
    31.     public float threshold = 0.001f;
    32.     Camera thisCamera;
    33.     int lineCount = 0;
    34.     private GameController theGC;
    35.  
    36.     Vector3 lastPos = Vector3.one * float.MaxValue;
    37.  
    38.     void Awake()
    39.     {
    40.         thisCamera = Camera.main;
    41.         lineRenderer = GetComponent<LineRenderer>();
    42.     }
    43.  
    44.     private void Start()
    45.     {
    46.         theGC = GameObject.Find("GameController").GetComponent<GameController>();
    47.         drawGizmos = true;
    48.         initiated = false;
    49.         spawned = true;
    50.         launched = false;
    51.         hasDetected = false;
    52.         selected = true;
    53.         Physics.IgnoreCollision(GameObject.Find("HitBox").GetComponent<BoxCollider>(), GetComponent<Collider>());
    54.     }
    55.  
    56.     void Update()
    57.     {
    58.         if (!launched)
    59.         {
    60.             Launch();
    61.         }
    62.         else
    63.         {
    64.             if (!hasDetected)
    65.             {
    66.                 GetComponent<CapsuleCollider>().radius = 1f;
    67.                 GetComponent<CapsuleCollider>().height = 70;
    68.                 //RotateToMousePosition();
    69.                 if (selected)
    70.                 {
    71.                     CalculatePath();
    72.                 }
    73.                 if (linePoints.Count > 0)
    74.                 {
    75.                     FollowPath();
    76.                     //InterpolatePath();
    77.                 }
    78.             }
    79.             if (hasDetected)
    80.             {
    81.                 RotateToMissile();
    82.             }
    83.         }
    84.         if (selected)
    85.         {
    86.             this.gameObject.transform.GetChild(0).gameObject.SetActive(true);
    87.             //Debug.Log(this.gameObject.transform.GetChild(0).gameObject);
    88.         }
    89.         else
    90.         {
    91.             deselected = true;
    92.             this.gameObject.transform.GetChild(0).gameObject.SetActive(false);
    93.         }
    94.         //Debug.Log(current);
    95.         //Debug.Log(linePoints.Count);
    96.         if (deselected && selected)
    97.         {
    98.             RotateToMousePosition();
    99.             linePoints.Clear();
    100.             current = 0;
    101.             deselected = false;
    102.         }
    103.     }
    104.  
    105.     void Launch()
    106.     {
    107.         dist = 0.2 - transform.position.y;
    108.         if (dist > 0.01)
    109.         {
    110.             transform.position = Vector3.Slerp(transform.position, new Vector3(transform.position.x, 0.2f, transform.position.z), moveSpeed * Time.deltaTime);
    111.         }
    112.         else
    113.         {
    114.             transform.localScale = new Vector3(0.01f, 0.004f, 0.02f);
    115.             launched = true;
    116.         }
    117.  
    118.     }
    119.  
    120.     private void OnTriggerEnter(Collider other)
    121.     {
    122.         if (other.tag == "LaunchedMissile")
    123.         {
    124.             hasDetected = true;
    125.             missileTarget = other.gameObject;
    126.         }
    127.     }
    128.  
    129.  
    130.  
    131.     void CalculatePath()
    132.     {
    133.         // Create objects in between since the mouse moves faster than the update!!
    134.         // Linear interpolation, beizel curve
    135.         Vector3 mousePos = Input.mousePosition;
    136.         //mousePos.z = thisCamera.nearClipPlane;
    137.         //Vector3 mouseWorld = thisCamera.ScreenToWorldPoint(mousePos);
    138.         int layerMask = 1 << 8; //We only want to raycast with the hitbox
    139.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    140.         RaycastHit hit;
    141.         if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask))
    142.         {
    143.             Vector3 mouseWorld = new Vector3(hit.point.x, hit.point.y, hit.point.z);
    144.             float dist = Vector3.Distance(lastPos, mouseWorld);
    145.             if (dist <= threshold)
    146.                 return;
    147.  
    148.             lastPos = mouseWorld;
    149.             if (linePoints == null)
    150.                 linePoints = new List<Vector3>();
    151.             linePoints.Add(mouseWorld);
    152.  
    153.             UpdateLine();
    154.         }
    155.  
    156.  
    157.     }
    158.  
    159.     void UpdateLine()
    160.     {
    161.         //lineRenderer.SetWidth(startWidth, endWidth);
    162.         //lineRenderer.SetVertexCount(linePoints.Count);
    163.  
    164.         lineRenderer.positionCount = linePoints.Count;
    165.  
    166.         //lineRenderer.startWidth = startWidth;
    167.         //lineRenderer.endWidth = endWidth;
    168.  
    169.  
    170.         for (int i = lineCount; i < linePoints.Count; i++)
    171.         {
    172.             lineRenderer.SetPosition(i, linePoints[i]);
    173.         }
    174.         lineCount = linePoints.Count;
    175.  
    176.         float distance = Vector3.Distance(transform.position, linePoints[linePoints.Count]);
    177.         lineRenderer.GetComponent<Renderer>().material.mainTextureScale = new Vector2(distance * 2, 1);
    178.  
    179.  
    180.     }
    181.  
    182.     void InterpolatePath()
    183.     {
    184.         for (int i = 1; i < linePoints.Count; i++)
    185.         {
    186.             //ry
    187.             //{
    188.             dist = Vector3.Distance(linePoints[i], linePoints[i + 1]);
    189.             Debug.Log(dist);
    190.             if (dist > 0.2)
    191.             {
    192.                 for (int j = 0; i < numPoints; i++)
    193.                 {
    194.                     float t = j / numPoints;
    195.                     linePoints.Insert(i + 1, CalculateLinearBezierpoint(t, linePoints[i], linePoints[i + j]));
    196.                 }
    197.             }
    198.             //} catch
    199.             //{
    200.  
    201.             //}
    202.         }
    203.     }
    204.  
    205.     private void DrawLinearCurve()
    206.     {
    207.         for (int i = 0; i < numPoints; i++)
    208.         {
    209.             float t = i / numPoints;
    210.         }
    211.     }
    212.  
    213.     private Vector3 CalculateLinearBezierpoint(float t, Vector3 p0, Vector3 p1)
    214.     {
    215.         return p0 + t * (p1 - p0);
    216.     }
    217.  
    218.     void FollowPath()
    219.     {
    220.         //transform.position = Vector3.Slerp(transform.position, testPos.transform.position, speed * Time.deltaTime);
    221.         try
    222.         {
    223.             float dist = Vector3.Distance(linePoints[current], transform.position);
    224.             transform.position = Vector3.Slerp(transform.position, linePoints[current], speed * Time.deltaTime);
    225.  
    226.             var rotation = Quaternion.LookRotation(linePoints[current] - transform.position);
    227.             transform.rotation = Quaternion.Slerp(transform.rotation, rotation, rotationSpeed * Time.deltaTime);
    228.             if (dist <= 0.01f)
    229.             {
    230.                 //Debug.Log(linePoints.Count);
    231.                 Debug.Log(linePoints.Remove(linePoints[current - 1]));
    232.                 linePoints.Remove(linePoints[current - 1]);
    233.                 current++;
    234.             }
    235.  
    236.  
    237.             if (linePoints.Count > 50 && (linePoints.Count - current) < 10)
    238.             {
    239.                 Instantiate(explosion, transform.position, Quaternion.identity);
    240.                 Destroy(gameObject);
    241.             }
    242.         }
    243.         catch
    244.         {
    245.             current++;
    246.         }
    247.     }
    248.  
    249.     private void SelfDestruct()
    250.     {
    251.         Instantiate(explosion, transform.position, Quaternion.identity);
    252.         Destroy(gameObject);
    253.     }
    254.  
    255.     private void OnDrawGizmos()
    256.     {
    257.         if (drawGizmos)
    258.         {
    259.             try
    260.             {
    261.                 for (int i = 0; i < linePoints.Count; i++)
    262.                 {
    263.                     //Debug.Log(i);
    264.                     Gizmos.DrawWireSphere(linePoints[i], (float)0.005);
    265.  
    266.                 }
    267.             }
    268.             catch
    269.             {
    270.  
    271.             }
    272.         }
    273.     }
    274.  
    275.  
    276.     void RotateToMousePosition()
    277.     {
    278.         ground = new Plane(Vector3.up, transform.position);
    279.  
    280.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    281.  
    282.         //*distance only needs an initial value to work
    283.         float dist = 0f;
    284.  
    285.         if (ground.Raycast(ray, out dist))
    286.         {
    287.             //create a vector position at latest raycast hit point
    288.             Vector3 mousePoint = new Vector3(ray.GetPoint(dist).x,
    289.                 transform.position.y, ray.GetPoint(dist).z);
    290.  
    291.             //*useful for showing where ray comes from in testing ONLY
    292.             Debug.DrawLine(ray.origin, mousePoint);
    293.  
    294.             //rotate object toward vector position
    295.             Quaternion targetRotation = Quaternion.LookRotation(mousePoint - transform.position);
    296.             transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
    297.  
    298.             //transform.rotation = new Quaternion(90, transform.rotation.z, transform.rotation.z, transform.rotation.w);
    299.             transform.position = Vector3.Slerp(transform.position, new Vector3(mousePoint.x, 0.2f, mousePoint.z), moveSpeed * Time.deltaTime);
    300.         }
    301.     }
    302.  
    303.     void RotateToMissile()
    304.     {
    305.         dist = Vector3.Distance(missileTarget.transform.position, transform.position);
    306.         //rotate object toward vector position
    307.         Quaternion targetRotation = Quaternion.LookRotation(missileTarget.transform.position - transform.position);
    308.         transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * 5 * Time.deltaTime);
    309.  
    310.         //transform.rotation = new Quaternion(90, transform.rotation.z, transform.rotation.z, transform.rotation.w);
    311.         transform.position = Vector3.Slerp(transform.position, new Vector3(missileTarget.transform.position.x, missileTarget.transform.position.y, missileTarget.transform.position.z), moveSpeed * 5 * Time.deltaTime);
    312.         if (dist < 0.02)
    313.         {
    314.             theGC.ShotDown++;
    315.             spawned = false;
    316.             Instantiate(explosion, transform.position, Quaternion.identity);
    317.             Destroy(missileTarget);
    318.             Destroy(gameObject);
    319.         }
    320.  
    321.     }
    322. }
    I hope I made some sense. To be honest I don't fully grasp materials/linerenderers so I've had some trouble wrapping my head around the issue.

    Thanks in advance for any help!