Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Question Create a new dot which automatically connects to line renderer and creates a polygon

Discussion in 'Scripting' started by VukPejovic, Feb 26, 2023.

  1. VukPejovic

    VukPejovic

    Joined:
    Nov 26, 2022
    Posts:
    2
    I am making a game which has dots in it which are connected by lines and create a polygon. You can drag them. When I click UpArrow key, it should create a new dot and add it to the polygon. Here teme and temena means dot and dots:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class LineController : MonoBehaviour
    6. {
    7.     private LineRenderer lr;
    8.     public List<Transform> temena;
    9.     public BrojTemenaPromena script;
    10.     private Vector3 spawnPoint = new Vector3(0, 0, 0);
    11.     //private Quaternion spawnQuaterion = new Quaternion(1, 0, 0, 0);
    12.     public GameObject objectToSpawn;
    13.     void Awake()
    14.     {
    15.         lr = GetComponent<LineRenderer>();
    16.     }
    17.     void Start()
    18.     {
    19.         lr = GetComponent<LineRenderer>();
    20.         SetUpLine(temena);
    21.     }
    22.     void Update()
    23.     {
    24.         StvaranjeTemena();
    25.         SetLinePosition(temena);
    26.     }
    27.  
    28.     public void SetUpLine(List<Transform> temena)
    29.     {
    30.         lr.positionCount = temena.Count + 1;
    31.         this.temena = temena;
    32.     }
    33.     public void SetLinePosition(List<Transform> temena)
    34.     {
    35.         for (int i = 0; i < temena.Count; i++)
    36.         {
    37.             lr.SetPosition(i, temena[i].position);
    38.         }
    39.         lr.SetPosition(temena.Count, temena[0].position);
    40.         this.temena = temena;
    41.     }
    42.     private void StvaranjeTemena()
    43.     {
    44.         if (Input.GetKeyDown(KeyCode.UpArrow))
    45.         {
    46.             Instantiate(objectToSpawn, spawnPoint, Quaternion.identity);
    47.             temena.Add(objectToSpawn.transform);
    48.             script.brojTemenaText.text = temena.Count.ToString();
    49.             SetUpLine(temena);
    50.             SetLinePosition(temena);
    51.         }
    52.     }
    53. }
    54.  
     
  2. VukPejovic

    VukPejovic

    Joined:
    Nov 26, 2022
    Posts:
    2
    Also, StvaranjeTemena() means creating dots/vertices