Search Unity

Question Is it possible to have a Collider that works as a trigger for some objects and normal for others?

Discussion in 'Physics' started by C4ustic, Feb 1, 2024.

  1. C4ustic

    C4ustic

    Joined:
    Dec 21, 2016
    Posts:
    5
    I have an object with a collider, lets say it is a wall in game. I have two types of projectiles, one bigger that I want to bounce normally on the trigger, give me the OnTriggerEnter event where I spawn a little bounce effect. Then I have a bullet projectile that I want to go straight through but I still need a callback like OnTriggerEnter to spawn a bullet hole effect.

    Is there any way to do this without having two colliders with different layers on the wall?
     
  2. C4ustic

    C4ustic

    Joined:
    Dec 21, 2016
    Posts:
    5
    I can add that I have a hack solution in place. What I do now is if I get a collision with a projectile that are supposed to go though, I turn off the collision for like 0.1s and put the projectile back one frame (I save priv frame pos, vel...). This is hacky and will result in the wrong kind of projectile going through from time to time.
     
  3. codebiscuits

    codebiscuits

    Joined:
    Jun 16, 2020
    Posts:
    90
    Can you rearrange this so that the wall is a regular collider, the big projectile has a regular collider, and the small projectile has a trigger collider?
    That way the big projectile will bounce off the wall and you'll get OnCollisionEnter at the wall;
    The small projectile will pass through the wall and you'll get OnTriggerEnter at the wall;
    ^^This works for me right now (both my projectiles have non-kinematic rigidbodies, wall does not - you may need to modify the contact pairs mode if you've got kinematic stuff going on), although maybe it's no good having the smaller projectile being a trigger?

    OnTriggerEnter won't give you the collision point for the bullethole, but you can perhaps cheat with the projectile position or something (as-is, interpolate, raycast along the velocity vector, Collider.ClosestPoint or something).

    Failing that, I don't think I'd feel too terrible about two colliders on different layers:)

    Or, you can probably do something clever with the contact modification, but I feel like that's probably overkill.
     
  4. C4ustic

    C4ustic

    Joined:
    Dec 21, 2016
    Posts:
    5
    Hmm, are you saying I should then be listening to the events on the projectile script side?

    My use case is a little more complicated than I described, I have other "concrete" walls where the small projectile shouldn't go through, but maybe my solution is to have multiple colliders on the projectile side. That would be a lot less work for me, unclear what is best for performance.

    I have never seen the contact modification callback thing, but at a first glance it seems like that might be exactly what I need, will look into it.

    Thanks for the reply!
     
    codebiscuits likes this.
  5. codebiscuits

    codebiscuits

    Joined:
    Jun 16, 2020
    Posts:
    90
    In my example I was listening on the wall, not the projectile; but i think you can also listen on the projectile.
    But either way, that wouldn't work with multiple wall types, sorry.
    I feel like a pair of colliders on different layers on the projectile is probably least work up front.
    I think if you then find there are performance issues, you can zero the max impulse (or maybe the contact separation) using contact modification: I think this will still give you the oncollision, but allow the projectile to pass through (not tested this tho, I'm on my phone now).
    You have to cache instance IDs and stuff for contact mod, I posted an example on here a week or two ago.
    Good luck!
     
  6. Ultrasnowy543

    Ultrasnowy543

    Joined:
    Dec 26, 2022
    Posts:
    13
    You Could Use Tags And The Bullets Could Have Different Tags And Then In OnTriggerEnter(Collider other)
    Do
    if(other.CompareTag("BigBullet"))

    if(other.CompareTag("SmallBullet"))
     
  7. C4ustic

    C4ustic

    Joined:
    Dec 21, 2016
    Posts:
    5
    That would not effect the physics bounce though right?