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

Make rigidbody ignore collisions from Physics.IgnoreCollision

Discussion in 'Physics' started by digiholic, Dec 20, 2016.

  1. digiholic

    digiholic

    Joined:
    Nov 23, 2013
    Posts:
    14
    I have a platformer game, and I am achieving the effects of one-way platforms by using Triggers to disable collisions between a platform and the player using Physics.IgnoreCollision. If I just use the PlayerController for collisions, the process works fine, but if I give the player a Rigidbody, I get some unexpected results.

    Whenever the player jumps up from below, after getting about halfway through, the Rigidbody pops them up the rest of the way until the bottom of the player is flush with the platform. It doesn't reset the vertical speed, though, so they then continue their jump from there, effectively getting a boost of half their height when jumping through a pass-through platform.

    Here's the code that calls ignoreCollision:

    Code (CSharp):
    1. void OnTriggerEnter(Collider other)
    2.     {
    3.         if (other.tag == "PassThrough")
    4.         {
    5.             Physics.IgnoreCollision(GetComponent<CharacterController>(), other.transform.parent.GetComponent<Collider>(), true);
    6.         }
    7.     }
    8.  
    9.     void OnTriggerExit(Collider other)
    10.     {
    11.         if (other.tag == "PassThrough")
    12.         {
    13.             Physics.IgnoreCollision(GetComponent<CharacterController>(), other.transform.parent.GetComponent<Collider>(), false);
    14.         }
    15.     }
    This script is attached to a character to give it the ability to jump through platforms (triggers with the "PassThrough" tag that are children of a platform). I've swapped the GetComponent CharacterController with Collider and Rigidbody to no effect.
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,390
    OnCollisionEnter is basically run after the physics system processes the collision... so the ignore collision is kicking in one frame late.
     
  3. digiholic

    digiholic

    Joined:
    Nov 23, 2013
    Posts:
    14
    The OnCollisionEnter is on a trigger, separate from the actual colliding platform, so it should be hitting the trigger well before the collision actually happens. I was aware of the one-frame delay so I made the trigger bigger than the collideable area of the parent platform.
     
  4. hoesterey

    hoesterey

    Joined:
    Mar 19, 2010
    Posts:
    659
    Are you sure its hitting a full frame before the collision happens? I've had issues with triggers as continous dynamic collision does not have the same effect with them as it does with actual colliers.

    Also is CharacterController a collider? I think you need to pass in the collider directly.
     
  5. digiholic

    digiholic

    Joined:
    Nov 23, 2013
    Posts:
    14
    I'm 100% certain that the pass-through trigger is more than a frame away. The trigger extends all the way to the ground below it, and the player doesn't even touch the platform until they've double-jumped.

    CharacterController is a collider, I get exactly the functionality I'm looking for as long as the player doesn't have a Rigidbody attached.
     
  6. hoesterey

    hoesterey

    Joined:
    Mar 19, 2010
    Posts:
    659
    Sounds like they have yet another bug in this system. There is another thread in this section about physics.ignorecollision not working. I tested this for them back in the beta for 5.5 and it was working then but have not converted my assets over to use this since. Not great news.
     
  7. digiholic

    digiholic

    Joined:
    Nov 23, 2013
    Posts:
    14
    If it helps figure out what's going on, or what a workaround could be here's a comparison:

    Here's the collision without a rigidbody: https://gfycat.com/FatalRealGermanshepherd
    And here it is with the exact same input, but a rigidbody added: https://gfycat.com/DeepFragrantBilby

    It's technically ignoring the collision, but it's popping the collider up to the top of the platform as soon as it's halfway through.