In this program, When i draw a hill, a car can road on it’s surface, and when I draw another one, the previous hill should remove.
but unfortunately the previous edge collider looks still there.
this is what i’ve done for delete this collider :
if(col)
{
col.enabled = false;
col.Reset();
Destroy(col.gameObject);
Destroy(col);
col = null;
}
unfortunately nothing works
this is the code:
i don’t know what i have to do to disable the previous collision
My Codes
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class LineFreeDraw : MonoBehaviour
{
private LineRenderer line;
private EdgeCollider2D col;
private bool isMousePressed;
private List<Vector2> pointsList;
private Vector2[] pointsArray;
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 = new GameObject("Line").AddComponent<LineRenderer>();
line.material = new Material(Shader.Find("Particles/Additive"));
line.SetVertexCount(0);
line.SetWidth(0.9f, 0.9f);
line.SetColors(Color.green, Color.green);
line.useWorldSpace = true;
isMousePressed = false;
pointsList = new List<Vector2>();
}
// -----------------------------------
void Update()
{
// If mouse button down, remove old line and set its color to green
if (Input.GetMouseButtonDown(0))
{
if(col)
{
col.enabled = false;
col.Reset();
Destroy(col.gameObject);
Destroy(col);
col = null;
}
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(new Vector2(mousePos.x, mousePos.y));
line.SetVertexCount(pointsList.Count);
line.SetPosition(pointsList.Count - 1, (Vector3)pointsList[pointsList.Count - 1]);
CarIsCollide();
}
}
}
private void CarIsCollide()
{
col = new GameObject("Collider").AddComponent<EdgeCollider2D>();
// Collider is added as child object of line
col.transform.parent = line.transform;
if (pointsList.Count < 2)
return;
pointsArray = new Vector2[] { pointsList[pointsList.Count - 2], pointsList[pointsList.Count - 1] };
col.points = pointsArray;
}
}