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

OnTriggerEnter only firing sometimes under exactly the same circumstances

Discussion in 'Scripting' started by raithza, Dec 14, 2011.

  1. raithza

    raithza

    Joined:
    Dec 14, 2011
    Posts:
    21
    Hi, fairly new to Unity so forgive any glaring mistakes :)

    I have an explosion modelled as a Sphere collider, IsTrigger true, with no rigidbody (I attached a kinematic rigidbody to be sure, but it made no difference), and a creep (enemy) object that has a Rigidbody and a normal Box Collider. An explosion object is instantiated when my bullet hits something, I can verify that it works and that the sphere collider does intersect the Box Collider. Sometimes, it works right, the Damage function is called for my creep, and the rigidbody gets nudged by the force. Other times nothing happens.

    This is my entire Explosion script:

    Code (csharp):
    1.  
    2. // Use this for initialization
    3.     void Start ()
    4.     {
    5.         Destroy(this,0.01f);
    6.     }
    7.    
    8.     void Update ()
    9.     {
    10.        
    11.     }
    12.    
    13.     void OnTriggerEnter (Collider collider)
    14.     {
    15.         if (!collider.isTrigger)
    16.         {      
    17.             CreepLogic creep = collider.GetComponent("CreepLogic") as CreepLogic;
    18.            
    19.             if (creep != null)
    20.             {
    21.                 collider.rigidbody.AddExplosionForce(10,this.transform.position,10,0,ForceMode.Impulse);
    22.                 creep.Damage(Damage);
    23.             }
    24.         }
    25.        
    26.     }
    27.  
    I have the destroy call in the start function to give the explosion a limited life, if there is a better way to do a check like this for one frame only please say so :)

    (I know I can use a tag as a safer way to check that the colliding entity is indeed a creep - but I don't think that is relevant).

    Is there anything I'm missing? Again, it works perfectly sometimes, other times not at all, for identical shots fired. If I add two enemies, it will work for both or neither (on the same explosion). In physics setting, I've set Sleep Velocity and Sleep Angular Velocity to 0. I can verify that the explosion does get instantiated (there are particles attached) and I've set it's sphere collider radius to 9001 just to be sure.

    I doubt whether it matters, but here is the line of code for where I instantiate the explosion. I can tell by the particle effect that it is at the location I desire. This is from the Bullet's script:
    Code (csharp):
    1. void OnTriggerEnter(Collider collider)
    2.     {
    3.         if (!collider.isTrigger)
    4.         {
    5.             var exp = Instantiate(Explosion,transform.position,transform.rotation);
    6.             (exp as GameObject).GetComponent<ExplosionLogic>().Damage = this.damage;
    7.             Destroy(gameObject, 1f);
    8.             Destroy(this);
    9.             if (transform.parent != null)
    10.             {
    11.                
    12.                 Destroy(transform.parent.gameObject, 1f);
    13.             }
    14.         }
    15.        
    16.        
    17.     }
     
  2. Evan-Greenwood

    Evan-Greenwood

    Joined:
    Aug 6, 2008
    Posts:
    98
    It might be an order of events thing?

    "OnTriggerEnter" happens in the physics simulation, which should be happening at a fixed timeStep (see: Project Settings/Time)... By default this is 0.02f.

    "Start" occurs just before the first update call. If it is set to Destroy itself 0.01f seconds after start it may not exist until the fixedTimeStep.

    You should probably use Physics.OverlapSphere instead in any case. It'll avoid some of the memory overhead of instantiating and destroying a collider and it'll be more reliable.
     
  3. raithza

    raithza

    Joined:
    Dec 14, 2011
    Posts:
    21
    Changing Destroy delay to 0.03f helped, thanks!