- Home /
LineRenderer & EdgeCollider problem
So I'm making a Unity game, one of its features being the player being able to draw a straight line with the LineRenderer. I also need to implement an EdgeCollider to the line created since it'll need to detect collisions with another object in the scene.
However, when I run the game, the line draws in the right place but the edge collider goes way off the screen and is much bigger than the line itself. Here's an example: 
Here's the code for line & edge collider, which is attached to the main camera (which is stationary):
using UnityEngine;
using System.Collections;
public class LineHandler : MonoBehaviour {
#region globals
public Color c1 = Color.yellow;
public Color c2 = Color.red; // c1 and c2 set the color of the line
private GameObject lineGO; // this game object holds the LineRenderer
private LineRenderer lineRenderer; // LineRenderer instance
private int i = 0; // integer used to store size and index of vertexes in the line
private bool pcSet; // boolean used to confirm whether or not a bc is there
PolygonCollider2D pc;
Vector2 startPoint;
#endregion
#region methods
void Start() // in this method we set up the necessary properties for the line
{
lineGO = new GameObject("Line"); // initialization of lineGO
lineGO.AddComponent<LineRenderer>(); // we give lineGO the LineRenderer
lineRenderer = lineGO.GetComponent<LineRenderer>(); // now lineRenderer knows what to refer to
lineRenderer.material = new Material(Shader.Find("Mobile/Particles/Additive")); // material assignment
lineRenderer.SetColors(c1, c2); // we set the start and end color of the line
lineRenderer.SetWidth(0.1f, 0.1f); // we set the start and end width of the line
lineRenderer.SetVertexCount(0); // we set the number of points in the line, the more we set, the bigger the line can become
}
void Update() //the line is created here
{
if (Input.touchCount > 0) // the touchCount property returns the number of touches onscreen, so this block of code execs only if the screen is touched
{
Touch touch = Input.GetTouch(0); // the touch variable keeps a reference to the player's touch
if(touch.phase == TouchPhase.Moved) // to move the line, the touch's phase needs to be Moved (Began/Moved/Stationary/Ended/Canceled)
{
#region handleLine
if (i == 0) // we increase the max number of vertexes in the line to two (start & end)
{
lineRenderer.SetVertexCount(1); //at the very beginning though, the line must only have 1 vertex (a point)
startPoint = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
}
else
{
lineRenderer.SetVertexCount(2);
}
Vector3 mPosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y,15); // mPosition is the position of the user's finger
lineRenderer.SetPosition(i, Camera.main.ScreenToWorldPoint(mPosition)); // i is the index of the point in question, mPosition is its position
if (i == 0)
{
i++; // the number of points increments only once
}
#endregion
#region handleCollider
if(!pcSet)
{
pc = lineGO.AddComponent<PolygonCollider2D>();
pcSet = true;
}
Vector2 endPoint = new Vector2(mPosition.x, mPosition.y);
Vector2 startPoint2 = new Vector2(startPoint.x,startPoint.y + 1);
Vector2 endPoint2 = new Vector2(endPoint.x,endPoint.y + 1);
pc.points = new Vector2[]{startPoint,endPoint,endPoint2,startPoint2};
#endregion
}
if(touch.phase == TouchPhase.Ended) // if the player removes his finger, this block executes
{
#region destroyLine
lineRenderer.SetVertexCount(0); //deletes the block
i = 0; //reinitializes i
#endregion
#region destroyCollider
PolygonCollider2D[] lineColliders = lineGO.GetComponents<PolygonCollider2D>(); // creates an array with any Colliders in there
foreach(PolygonCollider2D p in lineColliders) //destroys all Colliders
{
Destroy(p);
}
pcSet = false; // like this i confirm that there is no collider
#endregion
}
}
}
#endregion
}
I must also point out that for some reason the LineRenderer doesn't have a GetPosition, so I had to find a way to store the data from the start & end points of the lines.
Any advice on how to fix this? Thanks :D
Your answer
Follow this Question
Related Questions
Detecting if the object is hit by LineRenderer 1 Answer
Collider for Line Renderer? 5 Answers
Create a collider for area created by mouse 0 Answers
Internal collisions 1 Answer