0
\$\begingroup\$

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.

enter image description here

enter image description here

but unfortunately the previous edge collider looks still there. this is what i've done for delete this collider :

        if(col)
        {
            col.Reset();
            col = null;
            Destroy(col); 
        }

unfortunately nothing works :) col.enabled = false is also doesn't work

this is the code: i don't know what i have to do to disable the previous collision

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.Reset();
                col = null;
                Destroy(col); 
            }

            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;

    }

}
\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

Your code enters multiple Edge colliders and in the if loop only destroys the latest collider object.Check the following image, the collider of previous line still exist as child of the line game object

enter image description here

We need to delete each and every child in the Mouse button down event , which will fix the issue.

 if (Input.GetMouseButtonDown(0))
        {
            if(col)
            {
                col=null;

            }

            foreach (Transform child in line.transform) 
            {
                GameObject.Destroy(child.gameObject);
            }

            isMousePressed = true;
            line.SetVertexCount(0);
            pointsList.RemoveRange(0, pointsList.Count);
            line.SetColors(Color.green, Color.green);
        }
\$\endgroup\$
3
  • \$\begingroup\$ still doesn't work :( \$\endgroup\$ Commented Aug 23, 2015 at 3:44
  • \$\begingroup\$ do you think this issue is relating to the col = new GameObject("Collider").AddComponent<EdgeCollider2D>(); ? \$\endgroup\$ Commented Aug 23, 2015 at 3:51
  • \$\begingroup\$ Edited the answer :) \$\endgroup\$ Commented Aug 23, 2015 at 7:02

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.