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

OnTriggerStay2D Not Affecting Multiple Objects

Discussion in '2D' started by LiberLogic969, Apr 7, 2016.

  1. LiberLogic969

    LiberLogic969

    Joined:
    Jun 29, 2014
    Posts:
    138
    I'm having an issue with collisions in my game. I have a projectile that travels a set distance and then stops and remains stationary for 2 seconds. During this time any Enemies within its Collider are supposed to be damaged every x seconds. Enemies in my project have a HitManager component which uses OnTriggerStay2D for these "multi hit" projectiles and OnTriggerEnter2D for detecting traditional projectiles. The problem I'm having is when multiple enemies are clumped up together, one mulit-hit projectile will only affect one enemy in the group. The desired effect is that all of them should be damaged. I'm not sure if this is how OnTriggerStay2D is supposed to work. Has anyone dealt with this kind of issue before? If you need me to post code i will, its just that I may have to post quit a few components worth of it to get an idea of how things are working in my project.
     
  2. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    OnTriggerStay2D() is called each fixed update for every trigger that overlaps the collider area, so what you are seeing is not normal behavior.

    I actually recently had a shockingly similar scenario, where multiple colliders that hit an enemy at the same time behaved as if only one of them were colliding. I also had a system in place to manage persistent damage triggers and how often enemies were allowed to take damage from them, and the problem ended up being a logical error within this system.

    That said, it's probably something in code. Try putting a print(gameObject.GetInstanceID()) in the OnTriggerStay2D() definition in your enemies' scripts to verify that it actually is being called for each enemy. If you see that it is, but not all of them are taking damage, you'll know somethings up with your logic.
     
    LiberLogic969 likes this.
  3. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    Are you moving rigidbody or transform? It could make a difference. Changing transform.position moves the visual object, but moving rigidbody.position moves the physics object. They are some way linked together but i don't know how. If you want physics and collision/trigger events to work i would move the rigidbody.
     
  4. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    I wouldn't say that Transform.position is a visual component; it's the real position of the object in world space. It's just that Rigidbody.position is optimized for moving objects with Rigidbody's.
     
    escuedro likes this.
  5. LiberLogic969

    LiberLogic969

    Joined:
    Jun 29, 2014
    Posts:
    138
    All my projectiles have their own rigidbody2d and move by setting rigidbody2D.velocity = direction * speed; in the fixed update method.

    I debugged the stay event using the gameObject.InstanceID and it appears all the colliders are being detected like they should be. I also have a system that is used on persistent damage sources to make sure entities don't get damaged from the same source multiple times in a frame. The projectile I mentioned also has a timer which sets a flag that determines if it should damage entities (in this case it should damage them every 0.5 seconds). The bug was definitely part of this system. I've decided to remove the OnTriggerStay2D event from my HitManager component and instead I wrote a projectile behavior that uses Physics2D.OverlapCircle to damage entities in an area every x seconds. This will work better for future projectiles anyway!