Search Unity

Help with collision.

Discussion in 'Scripting' started by HariprasadA, Dec 18, 2017.

  1. HariprasadA

    HariprasadA

    Joined:
    Nov 14, 2017
    Posts:
    39
    I have a non trigger collider and a line renderer with a trigger collider.
    How do I check for collisions between the two and destroy my non trigger object on collision ?
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,332
    OnTriggerEnter. At least one of the objects (preferably the moving one) must have a rigidbody.

    The chart at the bottom of this page has the exact details of what collisions cause what messages to be sent.
     
  3. HariprasadA

    HariprasadA

    Joined:
    Nov 14, 2017
    Posts:
    39
    Code (csharp):
    1.  
    2. OnTriggerEnter(collider col)
    3. {
    4. if (col.gameobject.tag=="trail")
    5. gameobject.SetActive(false);
    6. }
    7.  
    I tried this and attached it to the moving object. But nothing happened.
     
  4. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    The collider with the trigger, and the gameobject script, has to have the collider set to "isTrigger".
     
  5. HariprasadA

    HariprasadA

    Joined:
    Nov 14, 2017
    Posts:
    39
    For me t
    For me the trail is a child of the object. I cant seem to make it work in any way. Can someone help ?
    It's a 2D game.
     
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,332
    Then you need to have 2D colliders, and name the method OnTriggerEnter2D.
     
  7. HariprasadA

    HariprasadA

    Joined:
    Nov 14, 2017
    Posts:
    39
    Code (csharp):
    1.  
    2. void OnTriggerEnter2D(Collider2D col)
    3.     {
    4.      
    5.         if (col.gameObject.tag=="Player"+playernumber) {
    6.             Debug.Log ("Not Dead");  
    7.         }
    8.         else
    9.         {
    10.             Debug.Log("Dead");
    11.         }
    12.  
    13.     }
    14.  
    worked but it always shows Dead.
    the if statement doesn't even work.