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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[Noob] Collision Trigger in Player or Coin ?

Discussion in '2D' started by camboui, Jul 8, 2015.

  1. camboui

    camboui

    Joined:
    Jul 8, 2015
    Posts:
    6
    Hi,

    I'm starting with unity and i have some questions.

    Is it better to check for collision in Player or in prefabs Coin ? (The player can catch coins.)


    Player :
    Code (CSharp):
    1.     void OnTriggerEnter2D(Collider2D obj)
    2.     {
    3.         switch (obj.tag) {
    4.         case "Coin":
    5.             Destroy(obj.gameObject);
    6.             Global.score++;
    7.             break;
    8.         }
    9.     }
    Coin :
    Code (CSharp):
    1. void OnTriggerEnter2D()
    2.     {
    3.             Destroy(gameObject);
    4.             Global.score++;
    5.     }
    And in the seconde case it increases score by 2. I guess because it takes both player and Coin but is there a mean to trigger this script only once when collision ?
    (Edit : Ok for this one, I thaught that unthicking a script would desactivate it :) )

    Is it bad for performance to let empty functions like : update{ } ?

    Is there a mean to show errors in MonoDevelop ?

    Why when a block is quite fast, it can pass through other rigid blocks ?


    Thanks !
    Have a nice day !
     
    Last edited: Jul 8, 2015
  2. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    Because physics updates happen in steps rather than continuously like in real life.
     
    camboui likes this.
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,563
    Yes, you can use Physics2D.changeStopsCallbacks to control this.

    Use Rigidbody2D.collisionDetectionMode set to continuous for fast moving objects. This is more expensive but guarantees that objects won't pass through each other.
     
    camboui likes this.
  4. camboui

    camboui

    Joined:
    Jul 8, 2015
    Posts:
    6
    Thanks for your replies, any idea for the other questions ? :)

    The first question
    and
    "Is it bad for performance to let empty functions like : update{ } ?"