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

OnTrigger doesn't activate sometimes for enemies

Discussion in 'Scripting' started by Patrizd002, Aug 3, 2020.

  1. Patrizd002

    Patrizd002

    Joined:
    May 13, 2020
    Posts:
    9
    I have an OnTrigger collider under my player that is supposed to be used to kill an enemy when it touches that collider. It usually works, but the problem is that if the player jumps and falls down really fast, the OnTrigger won't activate immediately and as a result, the player will get damaged as well as the enemy. What is the best way that I can take care of this problem?
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Maybe a raycast? You could also check when the player hits the enemy where they are located and see if they were still "squished"
     
  3. Patrizd002

    Patrizd002

    Joined:
    May 13, 2020
    Posts:
    9
    I did try a raycast, but it did not seem to be as accurate in damaging the enemy as the collider is.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,893
    What's the shape of the collider? You may be able to use a BoxCast, SphereCast or CapsuleCast if your collider's shape matches one of those, or maybe just approximate your character's shape with one of those.
     
  5. Patrizd002

    Patrizd002

    Joined:
    May 13, 2020
    Posts:
    9
    Using a boxcast seems to be working so far since I have not been able to replicate my problems yet. It is just about the same size as the box collider, so I'm wondering if I should either make the boxcast bigger or move the hitbox further away from the boxcast so that it will not cause the player to get hurt.
     
  6. mgrekt

    mgrekt

    Joined:
    Jun 22, 2019
    Posts:
    92
    idk about the collider part I have trouble with that too, but you are accessing a damage script right? That the player take the damage only?
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    If you move or rotate the rigidbody script with anything except .MovePosition or .MoveRotation, you will certianly open yourself to the possibility of missing trigger/collide calls.

    Any type of casting (box or otherwise) does not have this issue. But if you directly do
    transform.position = newPosition;
    or such, you're going to miss collisions/triggers.