- Home /
Question by
Tayfur · Apr 04, 2015 at 08:34 PM ·
collider2dlinerendererdrawline
How to add collider2D to LineRenderer?
Hello guys, Here is the code for drawing line in game. But I want gameobjects to collide with lines I draw in game. I think adding collider2D to LineRenderer will fix the problem. Can anyone help me ?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class DrawLine : MonoBehaviour {
private LineRenderer line;
private bool isMousePressed;
private List<Vector3> pointsList;
private Vector3 mousePos;
// Structure for line points
struct myLine
{
public Vector3 StartPoint;
public Vector3 EndPoint;
};
// -----------------------------------
void Awake()
{
// Create line renderer component and set its property
line = gameObject.AddComponent<LineRenderer>();
line.material = new Material(Shader.Find("Particles/Additive"));
line.SetVertexCount(0);
line.SetWidth(0.1f,0.1f);
line.SetColors(Color.green, Color.green);
line.useWorldSpace = true;
isMousePressed = false;
pointsList = new List<Vector3>();
}
void Update ()
{
// If mouse button down, remove old line and set its color to green
if(Input.GetMouseButtonDown(0))
{
isMousePressed = true;
line.SetVertexCount(0);
pointsList.RemoveRange(0,pointsList.Count);
line.SetColors(Color.green, Color.green);
}
else if(Input.GetMouseButtonUp(0))
{
isMousePressed = false;
}
// Drawing line when mouse is moving(presses)
if(isMousePressed)
{
mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
mousePos.z=0;
if (!pointsList.Contains (mousePos))
{
pointsList.Add (mousePos);
line.SetVertexCount (pointsList.Count);
line.SetPosition (pointsList.Count - 1, (Vector3)pointsList [pointsList.Count - 1]); } } }
Comment
Your answer
Follow this Question
Related Questions
Gameobject is drawing a line render,when it's not supposed to ! 0 Answers
C# Reset EdgeCollider2D.Points.Length and LineRenderer.Positions.Length to 0 0 Answers
Draw a round-shaped line 3 Answers
How to Change Draw Order for GL.LINES? 0 Answers
attach drawing line using (LineRenderer) to moving background 0 Answers