- Home /
Display LineRenderer over a plane instead of behind it
Hello,
I'm trying to draw a line between an object and the current mouse world position (3D). However, every time I move the mouse, I want the line to update to the new mouse position.
However, the problem comes, when I move the mouse too close to the camera. And the line begins to being cut inside the plane. I want the line to ALWAYS be at a higher position than the plane.
How can I avoid this from happening and have a line always in front of the plane?
private LineRenderer _line;
// Update the line when moving
void Update()
{
if (_line != null)
{
var screenPoint = Input.mousePosition;
screenPoint.z = 10; //distance of the plane from the camera
Vector3 mouseWorld = Camera.main.ScreenToWorldPoint(screenPoint);
_line.SetPosition(1, mouseWorld);
}
}
// Create the line
void OnMouseOver()
{
if (_line == null) {
if (Input.GetMouseButton(1)) {
_line = gameObject.AddComponent<LineRenderer>();
_line.positionCount = 2;
Vector3 mouseWorld = Camera.main.ScreenToViewportPoint(Input.mousePosition);
_line.SetPosition(0, this.transform.position);
_line.SetPosition(1, mouseWorld);
// Mouse position updates are on the Update() function
_line.SetWidth(0.1f, 0.1f);
}
}
}
Your answer

Follow this Question
Related Questions
Is it possible to have a gameObject as a point on a Line Render 1 Answer
What's the best way to draw a 2D line WITHOUT using LineRenderer? 1 Answer
Recalculate linerenderer when moving gameobjects 1 Answer
How can I raycast a mesh collider baked with LineRenderer.BakeMesh() ? Ty 1 Answer
Linerenderer: How to sample positions more frequently? 1 Answer