Search Unity

Resolved Render line between 2Vectors

Discussion in 'Scripting' started by MartinMa_, Aug 25, 2021.

  1. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
    Hi guys please i need your help.

    I want render line between 2 vectors with LineRenderer component but i recieving unexpected results probably because i do something wrong with screen to world position or something.

    i want connect Vector3 (0,0,0) with mouse clicked position i do it like this :

    I call this after mouse click :
    Code (CSharp):
    1.         void CalculateDirection()
    2.         {
    3.             Vector3 startPosition= new Vector3(0,0,0);
    4.             Vector3 mousePosition  = Camera.main.WorldToScreenPoint(Input.mousePosition);
    5.             Utils.DrawLine(cameraPosition,mousePosition,.1f);
    6.         }
    7.     }
    I render line in this method:
    Code (CSharp):
    1.      public static void DrawLine(Vector3 startPosition, Vector3 endPosition, float width)
    2.         {
    3.             UtilsManager uM = Object.FindObjectOfType<UtilsManager>();
    4.             var newLine = Object.Instantiate(uM.linePrefab).GetComponent<LineRenderer>();
    5.             newLine.SetPosition(0, startPosition);
    6.             newLine.SetPosition(1, endPosition);
    7.             newLine.startWidth = width;
    8.             newLine.endWidth = width;
    9.         }
    Result what i get is here :
    https://gifyu.com/image/G8pk

    I want line to always connect start vector(probably some transform.position i set),position of mouse click.
    Thanks for help in advance.
     
  2. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
  3. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455