- Home /
 
 
               Question by 
               MarcosValle · Oct 15, 2020 at 06:53 PM · 
                scripting problemtransformpositionlinerendererclick  
              
 
              Line render from an object to a click
I still can't solve this problem. I have a sprite that is in constant motion, and I want a line to be drawn from the object to where you click on the screen. I tried it with raycast and with screen to world point, however it gives me values contrary to the position where I click.
 void Update()
 {
     _line.SetPosition( 0 , transform.position );
     if( Input.GetMouseButtonDown(0) )
     {
         CastRay();
         _line.SetPosition( 1 , point );
     }
 }
 void CastRay ()
 {
     Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
     RaycastHit2D hit = Physics2D.Raycast( ray.origin , ray.direction , -10 );
     point = new Vector3( hit.point.x , hit.point.y , 0 );
 }
 
              
               Comment
              
 
               
              because your distance argument is negative for Physics2D.Raycast 
Your answer