- Home /
A line drawn with mouse using Linerenderer is not on exact position. How to fix it?,
I'm following a YouTube video and making a simple game where I can draw the line using the mouse position when I hold the left button down. But, the line drawn on the screen is not in the right position. I drew the line on point and the line appears at a certain offset. How do I fix it?
Here's a picture.
The red cross marks are where the actual mouse was and the black line was drawn above it. Is it possible to fix this?
Here's my code
Line.cs:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Line : MonoBehaviour { public LineRenderer lineRenderer; public EdgeCollider2D edgeCollider; public Rigidbody2D rb;
[HideInInspector] public List<Vector2> points = new List<Vector2>();
[HideInInspector] public int pointsCount = 0;
public float pointsMinDistance = 0.1f;
public float CircleColliderRadius;
public Vector2 GetLastPoint(){
return (Vector2)lineRenderer.GetPosition(pointsCount - 1);
}
public void UsePhysics(bool usePhysic){
rb.isKinematic = !usePhysic;
}
public void SetLineColor(Gradient LineColor){
lineRenderer.colorGradient = LineColor;
}
public void SetLineWidth(float width){
lineRenderer.startWidth = width;
lineRenderer.endWidth = width;
edgeCollider.edgeRadius = width/2f;
CircleColliderRadius = width/2f;
}
public void AddPoint(Vector2 newPoint){
if(pointsCount >= 1 && Vector2.Distance(newPoint, GetLastPoint()) < pointsMinDistance)
return;
points.Add(newPoint);
pointsCount++;
CircleCollider2D circleCollider = this.gameObject.AddComponent<CircleCollider2D>();
circleCollider.offset = newPoint;
circleCollider.radius = CircleColliderRadius;
lineRenderer.positionCount = pointsCount;
lineRenderer.SetPosition(pointsCount-1, newPoint);
if(pointsCount > 1)
edgeCollider.points = points.ToArray();
}
public void SetPointsMinDistance(float distance){
pointsMinDistance = distance;
}
}
LineDrawer.cs
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class LineDrawer : MonoBehaviour { public GameObject LinePrefabs; public LayerMask CantDrawOverLayer; int CantDrawOverLayerIndex;
public float LinePointsMinDistance;
public float LineWidth;
public Gradient LineColor;
public Rigidbody2D c1;
public Rigidbody2D c2;
Line currentLine;
private void Start(){
CantDrawOverLayerIndex = LayerMask.NameToLayer("CantDrawOver");
}
private void Update(){
if(Input.GetMouseButtonDown(0)){
BeginDraw();
}
if(currentLine != null)
Draw();
if(Input.GetMouseButtonUp(0)){
c1.gravityScale = 1f;
c2.gravityScale = 1f;
EndDraw();
}
}
public void BeginDraw(){
currentLine = Instantiate(LinePrefabs, this.transform).GetComponent<Line>();
currentLine.SetLineColor(LineColor);
currentLine.SetLineWidth(LineWidth);
currentLine.SetPointsMinDistance(LinePointsMinDistance);
currentLine.UsePhysics(false);
}
public void Draw(){
Vector2 MousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D hit = Physics2D.CircleCast(MousePos, LineWidth / 3f, Vector2.zero, 1f, CantDrawOverLayer);
if(hit)
EndDraw();
else
currentLine.AddPoint(MousePos);
}
public void EndDraw(){
if(currentLine != null){
if(currentLine.pointsCount < 2){
Destroy(currentLine.gameObject);
}
else{
currentLine.gameObject.layer = CantDrawOverLayerIndex;
currentLine.UsePhysics(true);
currentLine = null;
}
}
}
}
Any help would be appreciated. Thanks :),
Your answer
Follow this Question
Related Questions
Shoot on spheres 0 Answers
Arc line between two objects in world space 2 Answers
Line renderer; can you change the gradient's colorkeys/alphakeys location? 0 Answers
How to calculate inner vertices of a line renderer? (math question) 1 Answer
Function ScreenPointToLocalPointInRectangle returns different position values. 1 Answer