Physics.IgnoreCollision does not seem to work as expected

Hi all, I am attempting to fix a Levitation Bug in my game, however is proving quite difficult, for some reason the Physics.IgnoreCollision() function doesn’t seem to work as I expect it to work, I have also uploaded a video to better describe what is happening visually.


At the Start you can see it works quite well and all collision from the player is being ignored, while it is still able to crash into the fence… however whenever the player begins to move it seems like this function is being toggled on/off rapidly even though the only 2 ways the Collision can be Enable and Disabled with the player is by picking up the object, which disables collision with the player and by dropping the object, which then enables collision for the player and object.


There are no active Child Colliders on the objects (in the videos case the Server) as they are all disabled on start up besides the ‘main’ outside collider that is used to handle raycast from the player and collision for Physics. The code I am using to ask the object to ignore collisions from the player is as follows:

void OnMouseDown() //Pickup Object
    {
                if (pauseScript != null)
                {
                    if (!pauseScript.isPaused && !pcInt.isBusy)
                    {
                        if (isSmallOBJ)
                        {
                            distance = Vector3.Distance(transform.position, smallDest.position);
                        }
                        else
                        {
                            distance = Vector3.Distance(transform.position, dest.position);
                        }

                        if (canDisassemble)
                        {
                            disasseblyDeskScript.releaseTrigger();
                            canDisassemble = false;
                        }
                        if (isSelling)
                        {
                            psm.removeItemFromSale();
                        }
                        pcInt.dontShowIntText = true;
                        rig.useGravity = true;
                        rig.isKinematic = false;
                        isHolding = true;
                        rig.angularDrag = 7f;
                        rig.drag = 7f;
                        Physics.IgnoreCollision(mainCol, refer.playerCol, true); //Ignore is Requested here
                        pcInt.isHolding = true;
                        StartCoroutine(objCarryCheck());
                    }
                }
                else
                {
                    Debug.LogError("Pause Script is NULL! - This is stopping Objects from being picked up!");
                }
            }
        }
    }

public void dropObject() //Drop object
{
    rig.angularDrag = 0.05f;
    rig.drag = 0f;
    calculateObjectPrice();
    pcInt.dontShowIntText = false;
    isHolding = false;
    pcInt.isHolding = false;
    Physics.IgnoreCollision(mainCol, refer.playerCol, false); //Collision is now allowed after dropping
    if (camCon != null)
    {
        camCon.lowerVerticalLimit = defaultLowerLimit;
    }
    else
    {
        camCon = Char.transform.parent.GetComponent<CameraController>();
        camCon.lowerVerticalLimit = defaultLowerLimit;
    }

    timeToDisablePhysics = defaultDisableTime;
    physicsDisabled = false;
    if (refer.objectsActive != null)
    {
        if (!hasAdded)
        {
            refer.addObjectToList(gameObject);
            hasAdded = true;
            Debug.Log(itemName + " was successfully added to the Reference List - The Reference List Currently Contains: " + refer.objectsActive.Count + " Objects");
        }
    }
}

I found my mistake, I hadn’t realised the Character Controller Asset I have used uses a Raycast under the character to detect the ground and this was what was causing the Character to lift up whenever the object touched the players Ray.

Along with the Physics.IgnoreCollision() function I am also changing the objects layer from default to Ignore Raycast, while the object is being carried, I also then created a small timer after dropping the object to return the layer back to Default so the player cannot continuously spam Pickup/Drop the object to slowly levitate higher and higher.