Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

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,319
    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!