Search Unity

Resolved How to create a line using line renderer between two vector2int points in a grid

Discussion in '2D' started by Elmo_M, Mar 2, 2021.

  1. Elmo_M

    Elmo_M

    Joined:
    Oct 26, 2020
    Posts:
    3
    Hi,
    How would I create a line between two vector2int points using the line renderer (or something else)?

    I want to connect 2 items using their vector2int positions in a grid inventory with a visual line.
    Most tutorials I saw use a Transform[] to determine the points but i dont know how to implement this with vector2int points.

    Thank you! im fairly new with Unity and C#.
     
  2. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,511
    You need to convert from the grid's space to world space.
    Code (CSharp):
    1. Vector3 worldPos = myGrid.CellToWorld((Vector3Int)myVector2Int);
    Then give your world points to the LineRenderer.

    edit: you mention inventory.. when you say Grid you mean the component or are you using a Canvas and this UI?
     
    Last edited: Mar 2, 2021
  3. Elmo_M

    Elmo_M

    Joined:
    Oct 26, 2020
    Posts:
    3
    Thank you!