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.

Bug Physics.IgnoreCollision doesn't work at all.

Discussion in 'Editor & General Support' started by modernator24, May 9, 2021.

  1. modernator24

    modernator24

    Joined:
    Apr 7, 2017
    Posts:
    159
    I have a player character that has a CharacterController component. When the player shoots the gun, a case ejected, and this ejected case spawns inside of CharacterController's colliding area.

    This causes the player is jiggled by an ejected case, and also interrupts the player's movement, so I added Physics.IgnoreCollision to ignore physics calculation between them.

    Here's the code I'm using:
    Code (CSharp):
    1.  
    2. void EjectCasing() {
    3.     GameObject ejectedCaseObj = PoolManager.Instance.Get("Case", caseSpawnPoint.position, caseSpawnPoint.rotation);
    4.     PooledCase ejectedCase = ejectedCaseObj.GetComponent<PooledCase>();
    5.     Physics.IgnoreCollision(ejectedCase.Collider, Player.Instance.Collider);
    6.  
    7.     // Extra code for pushing ejected casing
    8. }
    It seems worked the first time, but it wasn't. Now the player can move without interrupted by ejected case, but at the moment the casing spawned, temporarily player's movement is interrupted and it's fairly noticeable(makes player very slow), especially using the weapon which generates lots of casings in a very short time, like machine guns.

    You may think this caused by something different, but it's not. When the casing isn't spawned, this issue never happens. The casing object and the player only have one collider for each(note: the player has CharacterController, afaik it's inherited Collider class).

    So I added two layers for players and ejected casings, named "Player" and "VFX" layers, and applied them each, and go to the physics settings, turn off the physics calculation of Player and VFX completely.

    But still, spawned casing disturbing player's movement. This issue existed since I used Unity 5.6.x and still it persists. Currently, I'm using Unity 2019.4.3f1 but seems like it's not fixed yet.

    How do I make the spawned casings don't interrupt the player's movement? Using Physics.IgnoreCollision and turning off the collision matrix didn't work at all.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,562
    Those methods work fine as far as I'm aware. You would need to share more information about your scene and how your objects are set up. It's possible you have extra colliders on the player or bullet by accident. Make sure to search for all colliders in the scene using the search bay in the hierarchy (search for "t: collider"). Make sure the layer of the actual objects that have the colliders are set appropriately (not parents or children of the colliders).

    Additionally, you can add OnCollisionEnter functions to the player to see exactly what object it is colliding with, if indeed a physics collision is the issue.
     
  3. modernator24

    modernator24

    Joined:
    Apr 7, 2017
    Posts:
    159
    I already searched if there any extra colliders multiple times by searching in the hierarchy but no, there is only a single collider per object. The layers are also correctly set.

    And I just put the OnControllerColliderHit to test there's something hit(OnCollisionEnter didn't work. Probably related CharacterController component) and tested, but no, the only ground is hit and no colliding with casings(even character is noticeable slowed).

    The code for checking hit something:

    Code (CSharp):
    1.  
    2. void OnControllerColliderHit(ControllerColliderHit hit)
    3.     {
    4.         print(hit.transform.name);
    5.     }
    6.  
    I attached screenshots for more explanations.
     

    Attached Files:

  4. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,314
    Is it possible to start the casings with their collider disabled, and then enable it after a short delay?
     
  5. modernator24

    modernator24

    Joined:
    Apr 7, 2017
    Posts:
    159
    Already tried with a coroutine, disable the collider right after instantiate, wait for a single frame, and enabled back, but the result was same.
     
  6. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,314
    What about before "instantiate"? Since you're pooling the casings anyway, instantiate it somewhere else, disable the collider, then move it into position.

    I'm surprised that turning the collision matrix off wouldn't work here, and it's making me suspicious that something else is at play -- even though evidence suggests otherwise.
     
  7. modernator24

    modernator24

    Joined:
    Apr 7, 2017
    Posts:
    159
    Well, it's actually reusing the pooled object, not instantiating every time. I just want to tell the object is generated somehow, not instantiating in the real. It's not a big deal, because it also happened before I used the pooling system.

    So do I.