Detect collision with ground

Hello, i am trying to detect if the player is colliding with the ground with a sphere cast but i don’t want it to collide with item because it cause player to fly (XR game) so i added a if comparetag = Ground but it’s still colliding with items and i can’t just turn of collision in project settings > physics because i need collisions for my sword etc to work can someone help me?

    private void FixedUpdate()
    {     
  bool isGrounded = CheckIfGrounded();
        if (isGrounded)
            fallingSpeed = 0;
        else
             fallingSpeed += gravity * Time.fixedDeltaTime;
        character.Move(Vector3.up * fallingSpeed * Time.fixedDeltaTime);
}

[…]

    bool CheckIfGrounded()
    {
        Vector3 rayStart = transform.TransformPoint(character.center);
        float rayLength = character.center.y + 0.01f;
        bool hasHit = Physics.SphereCast(rayStart, character.radius, Vector3.down, out RaycastHit hitInfo, rayLength, groundLayer);
        if(hitInfo.collider.CompareTag("Ground"))
        {
            return hasHit;
        }

            return false;
        }
    }

heres what i did. i created a empty object right under the player and added this code to the main player. all except for the variables under the void update()

public Transform GroundCheck;
    public float GroundDistance = 0.4f;
    public LayerMask GroundMask;
    bool IsGrounded;

IsGrounded = Physics.CheckSphere(GroundCheck.position, GroundDistance, GroundMask);

        if(IsGrounded && Velocity.y <0)
        {
           Velocity.y = -2f;

        }

after you did that you reference the empty game object into the script like this you just drag it in to the spot 6700336--769183--Screen Shot 2021-01-07 at 4.40.46 PM.png and it should work. also make sure that the game object and the player are connected kinda like this ( or you can just put the ground checker as a child of the player)6700336--769180--Screen Shot 2021-01-07 at 4.41.00 PM.png you also want to put all the things that are on the ground in a layer and set the layer mask to that same layer.

Thanks for the help but with a similar setup my character is not moving anymore. Is it not possible to do something to exclude certains tags ?

Yes, you can do this with layers, by passing a layerMask argument to the cast function.

The layerMask argument is a bitmask. This means it is individual bits rotated by the number of the layer.

Check out some Youtube tutorials on bitmasks… the reason it is done this way is so you can exclude or include multiple layers in one call.

I tried using bitmask but it is not working

    public bool CheckIfGrounded()
    {
        int bitmask = ~(1 << 11) & ~(1 << 12) & ~(1 << 13);
        Vector3 rayStart = transform.TransformPoint(character.center);
        float rayLength = character.center.y + 0.01f;
        bool hasHit = Physics.SphereCast(rayStart, character.radius, Vector3.down, out RaycastHit hitInfo, rayLength, bitmask);
                return hasHit;
}

I fixed my problem thanks for your answers

whats the answer?

The answer is certainly not to necro-post to two-year-old threads!!

Please start your own post. It’s FREE!

When you post, here is how to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, log output, variable values, and especially any errors you see
  • links to documentation you used to cross-check your work (CRITICAL!!!)

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379