I followed this and this tutorial, to code a Line which starts at a Game object and ends on the mouse position. But it doesn't work. My Mouse pointer is in the center, the line should be drawn from the rod end to the mouse:
If I look into the scene view, my rod string is always there, but always with some pixels difference. The ending of the 'Rod String' is where I go out of the game scene box:
Here's my Code:
using UnityEngine;
using System.Collections;
// Put this script on a Camera
public class DrawLines : MonoBehaviour
{
public GameObject mainPoint;
public Material lineMat;
void DrawConnectingLines()
{
Vector3 mouse = Input.mousePosition;
mouse.z = Camera.main.nearClipPlane;
Vector3 Worldpos = Camera.main.ScreenToWorldPoint(mouse);
Vector3 Worldpos2D = new Vector3(Worldpos.x, Worldpos.y, Worldpos.z);
Vector3 mainPointPos = mainPoint.transform.position;
GL.Begin(GL.LINES);
lineMat.SetPass(0);
GL.Color(new Color(lineMat.color.r, lineMat.color.g, lineMat.color.b, lineMat.color.a));
GL.Vertex3(mainPointPos.x, mainPointPos.y, mainPointPos.z);
GL.Vertex3(Worldpos2D.x, Worldpos2D.y, Worldpos2D.z);
GL.End();
}
// To show the lines in the game window whne it is running
void OnPostRender()
{
DrawConnectingLines();
}
// To show the lines in the editor
void OnDrawGizmos()
{
DrawConnectingLines();
}
}

