Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Kinematic and OnTrigger2D

Discussion in '2D' started by Fasertio41, Sep 18, 2017.

  1. Fasertio41

    Fasertio41

    Joined:
    Sep 13, 2017
    Posts:
    8
    Hello everyone :), in my game i have a kinematic rigidbody (my character) and a wall (kinematic). So, when my character touch the wall I want that my score increase by 1. But it don't work =(, so i have put a debug.log to debug the function but the problem is the OnTrigger2D function. This is my code, the function "ContaPunteggio()" work fine so the problem is OnTriggerEnter2D() :
    Code (CSharp):
    1.  void OntriggerEnter2D(Collider2D coll)
    2.     {
    3.         Debug.Log("Toccato");
    4.         if(coll.GetComponent<Aeroplano>()!= null)
    5.         {
    6.             GameControl.control.ContaPunteggio();
    7.            
    8.         }
    9.     }
     
  2. dccoo

    dccoo

    Joined:
    Oct 3, 2015
    Posts:
    161
    Watch out that you wrote the method with small T, the correct is OnTriggerEnter2D (in case the wall's collider is, in fact, a trigger)
    If your character really hits the wall (that is, he can't pass through it), then the wall has a collider that is not a trigger (what makes sense, since a trigger isn't meant to be collided with), so the OnTriggerEnter2D won't be called at all.
    Instead, if it's not a trigger, use OnCollisionEnter2D:
    Code (csharp):
    1.  
    2. void OnCollisionEnter2D(Collision2D coll) {
    3.     Debug.Log("Toccato");
    4.     if(coll.GetComponent<Aeroplano>()!= null){
    5.         GameControl.control.ContaPunteggio();
    6.     }
    7. }
    8.  
    PS: this:
    Code (csharp):
    1. if(a == b){
    is better than this:
    Code (csharp):
    1. if(a == b)
    2. {
    Jokes aside, hope that helps :)
     
    Last edited: Sep 20, 2017