Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Why the collision between line and object is not detected in unity c#?

Discussion in '2D' started by unity_2020828222, Oct 3, 2023.

  1. unity_2020828222

    unity_2020828222

    Joined:
    Jan 7, 2023
    Posts:
    3
    Hi Unity.. i have some problem i wanted to do a collision between a line drawn by the user with a checkpoint/object in 2d game.. But when the line is passing through the object it not debug any message that it already collided..

    You can view my game image and video from the link below
    https://drive.google.com/drive/folders/1OjEqGKxpcSkjEVROqQI-W-Vwk7w2uxfV?usp=drive_link

    I already try all possible combination.. methods also i already try both OnCollision and OnTriggered and i also try with and without rigidbody.. the triggered checked also i try..

    below is my code for the Checkpoint, Drawline and DrawnLineColliderUpdater.. I hope anyone can help me it so urgent.. sorry for my bad English..

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CP : MonoBehaviour
    6. {
    7.     /*
    8.     void OnTriggerEnter(Collider other)
    9.     {
    10.         Debug.Log("Checkpoint Collision Enter");
    11.         //Note: we use colliders here, not collisions
    12.         if (other.gameObject.tag == "DrawnLine")
    13.         {
    14.             Debug.Log("Collided");
    15.         }
    16.     }*/
    17.        
    18.         private void OnCollisionEnter2D(Collision2D collision)
    19.     {
    20.         Debug.Log("Checkpoint Collision Enter");
    21.         if (collision.gameObject.CompareTag("DrawnLine"))
    22.         {
    23.             Debug.Log("Collide!!");
    24.         }
    25.     }
    26. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DrawLine : MonoBehaviour
    6. {
    7.     private LineRenderer lineRenderer;
    8.     private bool isDrawing = false;
    9.  
    10.     void Start()
    11.     {
    12.         lineRenderer = GetComponent<LineRenderer>();
    13.         lineRenderer.positionCount = 0; // Start with an empty line.
    14.     }
    15.  
    16.     void Update()
    17.     {
    18.         // Check for user input to start/stop drawing.
    19.         if (Input.GetMouseButtonDown(0))
    20.         {
    21.             isDrawing = true;
    22.             lineRenderer.positionCount = 1; // Start with one point.
    23.             lineRenderer.SetPosition(0, GetMousePosition());
    24.         }
    25.         else if (Input.GetMouseButtonUp(0))
    26.         {
    27.             isDrawing = false;
    28.         }
    29.  
    30.         // Continue drawing while the mouse button is held down.
    31.         if (isDrawing && Input.GetMouseButton(0))
    32.         {
    33.             int newPositionCount = lineRenderer.positionCount + 1;
    34.             lineRenderer.positionCount = newPositionCount;
    35.             lineRenderer.SetPosition(newPositionCount - 1, GetMousePosition());
    36.         }
    37.     }
    38.  
    39.     Vector3 GetMousePosition()
    40.     {
    41.         // Get the mouse position in world space.
    42.         Vector3 mousePosition = Input.mousePosition;
    43.         mousePosition.z = 10f; // Set an appropriate z-value for your scene.
    44.         return Camera.main.ScreenToWorldPoint(mousePosition);
    45.     }
    46. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. [RequireComponent(typeof(LineRenderer))]
    7. public class DrawnLineColliderUpdater : MonoBehaviour
    8. {
    9.     private LineRenderer lineRenderer;
    10.     private PolygonCollider2D lineCollider;
    11.  
    12.     void Start()
    13.     {
    14.         lineRenderer = GetComponent<LineRenderer>();
    15.         lineCollider = transform.Find("ColliderContainer").GetComponent<PolygonCollider2D>();
    16.     }
    17.  
    18.     void Update()
    19.     {
    20.         // Copy LineRenderer's positions to the PolygonCollider2D's points.
    21.         Vector2[] colliderPoints = new Vector2[lineRenderer.positionCount];
    22.         for (int i = 0; i < lineRenderer.positionCount; i++)
    23.         {
    24.             colliderPoints[i] = lineRenderer.GetPosition(i);
    25.         }
    26.  
    27.         // Update the PolygonCollider2D's points.
    28.         lineCollider.points = colliderPoints;
    29.     }
    30. }
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    The documentation for PolygonColldier2D specifies that "The edge must enclose an area for the collider to work." You're going to have to make a collider with an area, instead of just a line.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,507
    As above, a line isn't a polygon. You'll see that the PolygonCollider2D will warn you in the inspector when you give it an invalid outline. Use an EdgeCollider2D instead. I would get familiar with the colliderd first before guessing at how they work in a real project.

    I hope you know that updating colliders per-frame is less than optimal, especially considering that physics doesn't even run per-frame by default.