Grounded Only on One Layer

I’ve got my character grounded properly, but I don’t want him considering standing on top of another person as ground. It’s hard to explain, but basically, the character flies. So when he stops flying, he’ll float down to the ground. My problem is that if he were fighting mid-air and accidentally collided above an enemy, he would act as if that enemy is the ground, and ground himself, thus stopping him from flying.

What I want him to do in that situation is to not consider that person as ground, and simply continue flying. So I essentially want to limit what the character see’s as being “grounded” to a selected layer mask. If anybody got through my mess of an explanation, here’s what I’m working with:

//Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
//Move controller
var controller:CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags & CollisionFlags.Below) != 0;
//^Here would be the “grounded only if colliding with layer X snippet”

Any help is greatly appreciated as always.

solution 1

make objects that should not collide in the same (different) layers, then using collision matrix disable collision between these two (with itself) layers.

solution 2

if other object is enemy, so i think (depends on gameplay) player and enemy should not collide at all, enemy must have a trigger to just detect collision and make some events if it happened. trigger will not stop collider falling.

solution 3

make “OnCollisionEnter” method for character and just check other collider for it’s ground flag. it can be just a Tag “Ground”. so only CollisionEnter with object tagged “Ground” will set flag “Grounded” to ‘true’, then use this flag to fall or don’t fall player down

 void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.CompareTag("Ground"))
        {
            isFlying = false;
        }
        //all other collidings should not set this flag to false, so only object tagged "Ground" can stop falling
    }