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

Best practices with colliders/triggers

Discussion in 'Getting Started' started by marciocdb, Dec 23, 2019.

  1. marciocdb

    marciocdb

    Joined:
    Dec 21, 2019
    Posts:
    1
    Hey guys
    I've been studying Unity for a few months now and one thing that still confuses me a bit is triggers for colliders.

    If you have a projectile and an object that it's suposed to hit, where is best to set the trigger? On the projectile or the object? Is there a "best practice" on this or varies for each case?

    Thanks a lot!
     
  2. BrandyStarbrite

    BrandyStarbrite

    Joined:
    Aug 4, 2013
    Posts:
    2,074
    Over the years, I've learned that there are a number of different ways, people would do this, in various game engines. But from what I know, put the trigger sensor on the object, that is going to get hit, by the bullet.
     
  3. Bill_Martini

    Bill_Martini

    Joined:
    Apr 19, 2016
    Posts:
    445
    First you should understand the difference between a trigger and a collider. Triggers do not activate physics. Use them for sensors to activate things like opening doors, spawning, starting animations, even changing scenes. Colliders use physics and will affect other objects.

    I put my code on the projectiles because I don't want to add it to 100s of environment objects. Projectile prefabs have the code already added and the number of projectiles are minimal as they are destroyed or pooled.
     
    DeanTheodorakis likes this.
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You don't necessarily even need a trigger for a projectile. You could use regular colliders for both the projectile and the object being hit. That's what I do unless I want the projectile to be able to pass through the target, or in a case where a raycast makes most sense.