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. Dismiss Notice

Question Detect collision with ground

Discussion in 'Scripting' started by Unity_Flowz, Jan 7, 2021.

  1. Unity_Flowz

    Unity_Flowz

    Joined:
    Jan 5, 2021
    Posts:
    10
    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?
    Code (CSharp):
    1.     private void FixedUpdate()
    2.     {    
    3.   bool isGrounded = CheckIfGrounded();
    4.         if (isGrounded)
    5.             fallingSpeed = 0;
    6.         else
    7.              fallingSpeed += gravity * Time.fixedDeltaTime;
    8.         character.Move(Vector3.up * fallingSpeed * Time.fixedDeltaTime);
    9. }
    [...]
    Code (CSharp):
    1.     bool CheckIfGrounded()
    2.     {
    3.         Vector3 rayStart = transform.TransformPoint(character.center);
    4.         float rayLength = character.center.y + 0.01f;
    5.         bool hasHit = Physics.SphereCast(rayStart, character.radius, Vector3.down, out RaycastHit hitInfo, rayLength, groundLayer);
    6.         if(hitInfo.collider.CompareTag("Ground"))
    7.         {
    8.             return hasHit;
    9.         }
    10.  
    11.             return false;
    12.         }
    13.     }
    14.  
    15.  
    16.  
     
  2. Henry_mullin

    Henry_mullin

    Joined:
    Jan 3, 2021
    Posts:
    13
    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()
    Code (CSharp):
    1. public Transform GroundCheck;
    2.     public float GroundDistance = 0.4f;
    3.     public LayerMask GroundMask;
    4.     bool IsGrounded;
    5.  
    6. IsGrounded = Physics.CheckSphere(GroundCheck.position, GroundDistance, GroundMask);
    7.  
    8.         if(IsGrounded && Velocity.y <0)
    9.         {
    10.            Velocity.y = -2f;
    11.  
    12.         }
    after you did that you reference the empty game object into the script like this you just drag it in to the spot 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) 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.
     
  3. Unity_Flowz

    Unity_Flowz

    Joined:
    Jan 5, 2021
    Posts:
    10
    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 ?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    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.
     
  5. Unity_Flowz

    Unity_Flowz

    Joined:
    Jan 5, 2021
    Posts:
    10
    I tried using bitmask but it is not working
    Code (CSharp):
    1.     public bool CheckIfGrounded()
    2.     {
    3.         int bitmask = ~(1 << 11) & ~(1 << 12) & ~(1 << 13);
    4.         Vector3 rayStart = transform.TransformPoint(character.center);
    5.         float rayLength = character.center.y + 0.01f;
    6.         bool hasHit = Physics.SphereCast(rayStart, character.radius, Vector3.down, out RaycastHit hitInfo, rayLength, bitmask);
    7.                 return hasHit;
    8. }
     
  6. Unity_Flowz

    Unity_Flowz

    Joined:
    Jan 5, 2021
    Posts:
    10
    I fixed my problem thanks for your answers
     
  7. PrimalEffect

    PrimalEffect

    Joined:
    May 6, 2022
    Posts:
    2
    whats the answer?
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    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://forum.unity.com/threads/using-code-tags-properly.143875/