Search Unity

Bug 2D collider made through script doesn't detect other 2D colliders

Discussion in '2D' started by LuisCruz98, Apr 3, 2021.

  1. LuisCruz98

    LuisCruz98

    Joined:
    Apr 11, 2020
    Posts:
    1
    Hi, I created a box collider between two points A and B. When another collider (enemy) enter my created collider it has to be destroyed but it doesn´t get destroyed. In fact, it doesn't even get registered that both game Object had collided.

    upload_2021-4-2_23-52-47.png

    upload_2021-4-2_23-54-23.png

    This is how I created the 2d box collider:

    Code (CSharp):
    1. public void addColliderToLine()
    2.     {
    3.         BoxCollider col = new GameObject("Collider").AddComponent<BoxCollider>();
    4.         col.transform.parent = line.transform; // Collider is added as child object of line
    5.         col.gameObject.tag = "Laser";
    6.         col.isTrigger = true;
    7.         // Following lines calculate the angle between startPos and endPos
    8.         float angle = (Mathf.Abs(startPos.position.y - endPos.position.y) / Mathf.Abs(startPos.position.x - endPos.position.x));
    9.         if ((startPos.position.y < endPos.position.y && startPos.position.x > endPos.position.x) || (endPos.position.y < startPos.position.y && endPos.position.x > startPos.position.x))
    10.         {
    11.             angle *= -1;
    12.         }
    13.         angle = Mathf.Rad2Deg * Mathf.Atan(angle);
    14.         col.transform.Rotate(0, 0, angle);
    15.         float lineLength = Vector3.Distance(startPos.position, endPos.position); // length of line
    16.         col.size = new Vector3(lineLength, 0.1f, 1f); // size of collider is set where X is length of line, Y is width of line, Z will be set as per requirement
    17.         Vector3 midPoint = (startPos.position + endPos.position) / 2;
    18.         col.transform.position = midPoint; // setting position of collider object
    19.     }
    And in my enemy script I have something like this:

    Code (CSharp):
    1.     private void OnTriggerEnter2D(Collider2D collision)
    2.     {
    3.         if (collision.CompareTag("Laser"))
    4.         {
    5.             Destroy(gameObject);
    6.         }
    7.     }
    The tags are in order and enemy has it's respective rigidBody.
     
  2. rarac

    rarac

    Joined:
    Feb 14, 2021
    Posts:
    570
    you didnt make a boxcollider2d
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    As stated above, you're using the 3D physics component BoxCollider. The 2D equivalent is BoxCollider2D.

    Personally I'd make it much simpler and use an EdgeCollider2D so you only need to set the start/end vertex and just add a small EdgeRadius. This way you can ignore all the positioning and rotating.
     
    LuisCruz98 and Deleted User like this.
  4. Deleted User

    Deleted User

    Guest

    Maybe you should also make sure the collision doesn't happen before the collider has been created.