Search Unity

Ignore physics collisions without disabling OnCollision effects

Discussion in '2D' started by Zek23, Jul 11, 2015.

  1. Zek23

    Zek23

    Joined:
    Mar 23, 2015
    Posts:
    21
    So I have a player character and an enemy that both have box colliders, and I want them to pass through eachother. So within OnCollisionEnter2D I added a call to Physics2D.IgnoreCollision() between the two colliders. The problem is that this causes the collision handler to only be called once and never again, which means the player doesn't take damage the next time they run into the enemy. Is there a way I can get Unity to allow those specific colliders to pass through eachother but still trigger the collision handlers when they do?
     
  2. itzclay36

    itzclay36

    Joined:
    Apr 11, 2015
    Posts:
    25
    Set your collider to "trigger". and get rid of the "Ignore Collision". Trigger is setup specifically for this.

    Put your code inside "void OnTriggerEnter2D" instead of OnCollide
     
    SAMYTHEBIGJUICY likes this.
  3. Zek23

    Zek23

    Joined:
    Mar 23, 2015
    Posts:
    21
    Yes but I do want it to collide normally with other objects. Can a trigger be made to behave like a normal collider for certain collisions?
     
  4. itzclay36

    itzclay36

    Joined:
    Apr 11, 2015
    Posts:
    25
    I do believe you are going about things the wrong way, but it's hard for me to say. My apologies if I'm not speaking towards the problem. But:

    In the game I'm just finishing right now, my main character has about 10 different colliders on it. Most of the colliders are triggers, with 1 object being a collider that collides with objects physically.

    The physical collider is the object that gets the rigidbody.

    The rest of my colliders are all just triggers and detectors for different things. They are in their own objects and childs of the main character. These all have ontrigger scripts that perform different functions(auto attacks etc).

    Additonally, these detectors get put in different layers, and then with the physics2d layer setting, I set some triggers to only interact with certain layers. My attack detector will only trigger with enemy layer, while my ground detector will only trigger with platforms.

    I believe your solution lies in something similar to this, rather than trying to accomplish so many things with a single collider object.
     
    Last edited: Jul 12, 2015
  5. itzclay36

    itzclay36

    Joined:
    Apr 11, 2015
    Posts:
    25
    Also, when it comes to wanting to collide with an object 1 minute, but not the next, changing layers is what I do.

    For example, I am able to jump through some platforms and then land on top. In order to do this, I need for the player to not collide with the platform on the way up, but still collide on the way down.

    I accomplish this by creating a layer in my project called "PlayerJumping". Then in the layers settings I have PlayerJumping set to not collide with the "TopOnlyPlatform" layer.

    Inside the player with the main collider and the rigidbody, inside the FixedUpdate, I put in some code that basically says if the player is moving in the up direction, then it moves the player into the "PlayerJumping" layer, allowing it to pass through the collider. When it sees that the player is not moving up, then it switches back to the normal "Player" layer, which will collide and allow the player to stand on the platform it just jumped through.

    I also have to detect and make sure the player isn't inside the collider when switching back so it doesn't get stuck, but I think you get the idea on changing layers for collisions.
     
    Maloke likes this.
  6. relser99

    relser99

    Joined:
    Jul 10, 2013
    Posts:
    4
    In case you haven't solved this, or anyone else comes along hoping for a good solution to this problem, I solved a similar problem: in a platformer I made, I wanted my character to be able to jump up through a platform if they're below it, but then stand upon it if they're above it. Just like itzclay36's problem in their last post.

    My solution is to add a second collider set to trigger mode. Place the trigger collider some distance away from your physical collider. In my case, the trigger collider went just below the physical collider that formed the surface of a platform. In your case, the trigger collider might be the same shape and position as your physical collider, but larger so its edges extend beyond the physical collider, causing colliding objects to touch the trigger before they touch the physical collider. Note that you'll need your trigger collider to extend far enough from your physical collider that objects which touch the trigger collider won't reach the physical collider on the same frame.

    Now, in your OnTriggerEnter function, if the triggering collider's gameobject is your player, disable collision detection between the physical collider and that gameobject. Similarly, in OnTriggerExit function, re-enable collision detection if the gameobject matches.

    Alternately, you could invert this solution so that collisions are disabled by default, and only enabled when the trigger is touched first.
     
    LearningStart likes this.
  7. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Have you looked at physics layers? You can have stuff with colliders & just set the physics layers so they always ignore each other.