Search Unity

OnTriggerEnter2D isn't working

Discussion in 'General Discussion' started by Senkrigar, Aug 3, 2019.

  1. Senkrigar

    Senkrigar

    Joined:
    Jul 17, 2019
    Posts:
    8
    I am making a game on Android where you can draw straight lines, that acts as platforms, here's the code for this :
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class LineSpawn : MonoBehaviour
    6. {
    7.     Vector2 firstPosition;
    8.     Vector2 movePosition;
    9.     LineRenderer lr;
    10.     GameObject line;
    11.     public float lineWidth = 5f;
    12.     public int lineRoundCorners = 2;
    13.     private BoxCollider2D lineCollider;
    14.     private Touch touch;
    15.  
    16.     void Start()
    17.     {
    18.        
    19.     }
    20.  
    21.     void Update()
    22.     {
    23.         if (Input.touchCount > 0)
    24.         {
    25.             touch = Input.GetTouch(0);
    26.  
    27.             if (touch.phase == TouchPhase.Began)
    28.             {
    29.                 Destroy(line);
    30.                 lr = null;
    31.  
    32.                 firstPosition = Camera.main.ScreenToWorldPoint(touch.position);
    33.                 movePosition = firstPosition;
    34.  
    35.                 line = new GameObject("Platform");
    36.  
    37.  
    38.                 lr = line.AddComponent<LineRenderer>();
    39.                 lr.enabled = true;
    40.                 lr.positionCount = 2;
    41.                 lr.SetPosition(0, firstPosition);
    42.                 lr.SetPosition(1, firstPosition);
    43.                 lr.useWorldSpace = true;
    44.                 lr.numCapVertices = lineRoundCorners;
    45.                 lr.widthMultiplier = lineWidth;
    46.                 lr.startColor = Color.green;
    47.                 lr.endColor = Color.green;
    48.                 lr.sortingOrder = 1;
    49.                 lr.material = new Material(Shader.Find("Sprites/Default"));
    50.                
    51.             }
    52.  
    53.             else if (touch.phase == TouchPhase.Moved)
    54.             {
    55.                 movePosition = Camera.main.ScreenToWorldPoint(touch.position);
    56.  
    57.                 lr.SetPosition(1, movePosition);
    58.  
    59.  
    60.  
    61.             }
    62.  
    63.             else if (touch.phase == TouchPhase.Ended)
    64.             {
    65.  
    66.                 AddColliderToLine(lr, firstPosition, movePosition);
    67.  
    68.             }
    69.         }
    70.        
    71.     }
    72.  
    73.     private void AddColliderToLine(LineRenderer line, Vector2 startPoint, Vector2 endPoint)
    74.     {
    75.         lineCollider = new GameObject("LineCollider").AddComponent<BoxCollider2D>();
    76.         lineCollider.transform.parent = line.transform;
    77.         float lineWidth = line.endWidth;
    78.         float lineLength = Vector2.Distance(startPoint, endPoint);
    79.         lineCollider.size = new Vector2(lineLength, lineWidth);
    80.         Vector2 midPoint = (startPoint + endPoint) / 2;
    81.         lineCollider.transform.position = midPoint;
    82.  
    83.        
    84.         float angle = Mathf.Atan2((endPoint.y - startPoint.y), (endPoint.x - startPoint.x));
    85.        
    86.         angle *= Mathf.Rad2Deg;
    87.  
    88.         lineCollider.transform.Rotate(0, 0, angle);
    89.     }
    90.    
    91. }
    92.  
    Now, I wanna add an enemy that you can kill by drawing a line on it, here the code :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnnemyScript : MonoBehaviour
    6. {
    7.     void Start()
    8.     {
    9.  
    10.     }
    11.  
    12.     void Update()
    13.     {
    14.        
    15.     }
    16.  
    17.     private void OnTriggerEnter2D(Collider2D other)
    18.     {
    19.             Debug.Log("TOUCHING");
    20.     }
    21. }
    22.  
    23.  
    The OnTriggerEnter2D works fine with any other Colliders in my game, but it doesn't detect the BoxCollider2D LineCollider, even when I can see in Unity that the Collider2D of the ennemy and the line are touching each other. Does anyone have an idea of why this happens ?
    Thank you for your help ! :)
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296