Detect if the player is landed on top of a box with rigidbody












2















I'm working on a Unity game where the player shoots on cubes to change their weight (it can be positive or negative, last one meaning 'falling' to the roof) and i'm experiencing a problem with rigidbodies, spherecasting and detecting if the player is grounded or not.



When my player is on the ground or on top of any object with a collider, I detect it as 'grounded' using the following function:



if (Physics.SphereCast(transform.position, m_Capsule.radius, Vector3.down, out hitInfo, 
((m_Capsule.height / 2f) - m_Capsule.radius) + advancedSettings.groundRoofCheckDistance, Physics.AllLayers, QueryTriggerInteraction.Ignore))
{
m_IsGrounded = true;
}


where advancedSettings.groundRoofCheckDistance is set to 0.01f.



Until there, everything works fine. But now, when I try to get on top of a cube with a non-kinematic rigidbody, I can't get that boolean to be true.



Here are two captures to illustrate my problem :



In this one, the player is falling on a non-kinematic rigidbody box and the boolean circled in red is m_isGrounded (false):



enter image description here



And here, same but the cube is kinematic and the ground is detected just fine:



enter image description here



I really can't figure out why the rigidbodies do that, or if I have a problem with my ground detection function, so any help is welcome.
Thanks!



PS: I'm using Unity 2018.2.15f1










share|improve this question





























    2















    I'm working on a Unity game where the player shoots on cubes to change their weight (it can be positive or negative, last one meaning 'falling' to the roof) and i'm experiencing a problem with rigidbodies, spherecasting and detecting if the player is grounded or not.



    When my player is on the ground or on top of any object with a collider, I detect it as 'grounded' using the following function:



    if (Physics.SphereCast(transform.position, m_Capsule.radius, Vector3.down, out hitInfo, 
    ((m_Capsule.height / 2f) - m_Capsule.radius) + advancedSettings.groundRoofCheckDistance, Physics.AllLayers, QueryTriggerInteraction.Ignore))
    {
    m_IsGrounded = true;
    }


    where advancedSettings.groundRoofCheckDistance is set to 0.01f.



    Until there, everything works fine. But now, when I try to get on top of a cube with a non-kinematic rigidbody, I can't get that boolean to be true.



    Here are two captures to illustrate my problem :



    In this one, the player is falling on a non-kinematic rigidbody box and the boolean circled in red is m_isGrounded (false):



    enter image description here



    And here, same but the cube is kinematic and the ground is detected just fine:



    enter image description here



    I really can't figure out why the rigidbodies do that, or if I have a problem with my ground detection function, so any help is welcome.
    Thanks!



    PS: I'm using Unity 2018.2.15f1










    share|improve this question



























      2












      2








      2








      I'm working on a Unity game where the player shoots on cubes to change their weight (it can be positive or negative, last one meaning 'falling' to the roof) and i'm experiencing a problem with rigidbodies, spherecasting and detecting if the player is grounded or not.



      When my player is on the ground or on top of any object with a collider, I detect it as 'grounded' using the following function:



      if (Physics.SphereCast(transform.position, m_Capsule.radius, Vector3.down, out hitInfo, 
      ((m_Capsule.height / 2f) - m_Capsule.radius) + advancedSettings.groundRoofCheckDistance, Physics.AllLayers, QueryTriggerInteraction.Ignore))
      {
      m_IsGrounded = true;
      }


      where advancedSettings.groundRoofCheckDistance is set to 0.01f.



      Until there, everything works fine. But now, when I try to get on top of a cube with a non-kinematic rigidbody, I can't get that boolean to be true.



      Here are two captures to illustrate my problem :



      In this one, the player is falling on a non-kinematic rigidbody box and the boolean circled in red is m_isGrounded (false):



      enter image description here



      And here, same but the cube is kinematic and the ground is detected just fine:



      enter image description here



      I really can't figure out why the rigidbodies do that, or if I have a problem with my ground detection function, so any help is welcome.
      Thanks!



      PS: I'm using Unity 2018.2.15f1










      share|improve this question
















      I'm working on a Unity game where the player shoots on cubes to change their weight (it can be positive or negative, last one meaning 'falling' to the roof) and i'm experiencing a problem with rigidbodies, spherecasting and detecting if the player is grounded or not.



      When my player is on the ground or on top of any object with a collider, I detect it as 'grounded' using the following function:



      if (Physics.SphereCast(transform.position, m_Capsule.radius, Vector3.down, out hitInfo, 
      ((m_Capsule.height / 2f) - m_Capsule.radius) + advancedSettings.groundRoofCheckDistance, Physics.AllLayers, QueryTriggerInteraction.Ignore))
      {
      m_IsGrounded = true;
      }


      where advancedSettings.groundRoofCheckDistance is set to 0.01f.



      Until there, everything works fine. But now, when I try to get on top of a cube with a non-kinematic rigidbody, I can't get that boolean to be true.



      Here are two captures to illustrate my problem :



      In this one, the player is falling on a non-kinematic rigidbody box and the boolean circled in red is m_isGrounded (false):



      enter image description here



      And here, same but the cube is kinematic and the ground is detected just fine:



      enter image description here



      I really can't figure out why the rigidbodies do that, or if I have a problem with my ground detection function, so any help is welcome.
      Thanks!



      PS: I'm using Unity 2018.2.15f1







      c# unity3d rigid-bodies






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 '18 at 17:25









      Programmer

      77k1089158




      77k1089158










      asked Nov 19 '18 at 15:25









      Studio PieStudio Pie

      114




      114
























          1 Answer
          1






          active

          oldest

          votes


















          1














          There are many ways to detect if player is grounded or not. If raycast, SphereCast and other ray based detection API are not working properly, try something different. Use the callback functions such as OnCollisionEnter and OnCollisionExit with flag. Check that flag in the Update function.



          bool m_IsGrounded;

          void OnCollisionEnter(Collision collision)
          {
          if (collision.collider.CompareTag("Ground"))
          m_IsGrounded = true;
          }

          void OnCollisionExit(Collision collision)
          {
          if (collision.collider.CompareTag("Ground"))
          m_IsGrounded = false;
          }

          void Update()
          {
          if (m_IsGrounded)
          {
          Debug.Log("Grounded");
          }
          }


          Note that this is checking for the "Ground" tag so your ground object must be on the "Ground" tag. You have to manually create this tag from the Editor then change your ground object tag to this.






          share|improve this answer
























          • I tried that, spent the day on adapting rest of my code but it now works. Thanks.

            – Studio Pie
            Nov 20 '18 at 18:05











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53377778%2fdetect-if-the-player-is-landed-on-top-of-a-box-with-rigidbody%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          There are many ways to detect if player is grounded or not. If raycast, SphereCast and other ray based detection API are not working properly, try something different. Use the callback functions such as OnCollisionEnter and OnCollisionExit with flag. Check that flag in the Update function.



          bool m_IsGrounded;

          void OnCollisionEnter(Collision collision)
          {
          if (collision.collider.CompareTag("Ground"))
          m_IsGrounded = true;
          }

          void OnCollisionExit(Collision collision)
          {
          if (collision.collider.CompareTag("Ground"))
          m_IsGrounded = false;
          }

          void Update()
          {
          if (m_IsGrounded)
          {
          Debug.Log("Grounded");
          }
          }


          Note that this is checking for the "Ground" tag so your ground object must be on the "Ground" tag. You have to manually create this tag from the Editor then change your ground object tag to this.






          share|improve this answer
























          • I tried that, spent the day on adapting rest of my code but it now works. Thanks.

            – Studio Pie
            Nov 20 '18 at 18:05
















          1














          There are many ways to detect if player is grounded or not. If raycast, SphereCast and other ray based detection API are not working properly, try something different. Use the callback functions such as OnCollisionEnter and OnCollisionExit with flag. Check that flag in the Update function.



          bool m_IsGrounded;

          void OnCollisionEnter(Collision collision)
          {
          if (collision.collider.CompareTag("Ground"))
          m_IsGrounded = true;
          }

          void OnCollisionExit(Collision collision)
          {
          if (collision.collider.CompareTag("Ground"))
          m_IsGrounded = false;
          }

          void Update()
          {
          if (m_IsGrounded)
          {
          Debug.Log("Grounded");
          }
          }


          Note that this is checking for the "Ground" tag so your ground object must be on the "Ground" tag. You have to manually create this tag from the Editor then change your ground object tag to this.






          share|improve this answer
























          • I tried that, spent the day on adapting rest of my code but it now works. Thanks.

            – Studio Pie
            Nov 20 '18 at 18:05














          1












          1








          1







          There are many ways to detect if player is grounded or not. If raycast, SphereCast and other ray based detection API are not working properly, try something different. Use the callback functions such as OnCollisionEnter and OnCollisionExit with flag. Check that flag in the Update function.



          bool m_IsGrounded;

          void OnCollisionEnter(Collision collision)
          {
          if (collision.collider.CompareTag("Ground"))
          m_IsGrounded = true;
          }

          void OnCollisionExit(Collision collision)
          {
          if (collision.collider.CompareTag("Ground"))
          m_IsGrounded = false;
          }

          void Update()
          {
          if (m_IsGrounded)
          {
          Debug.Log("Grounded");
          }
          }


          Note that this is checking for the "Ground" tag so your ground object must be on the "Ground" tag. You have to manually create this tag from the Editor then change your ground object tag to this.






          share|improve this answer













          There are many ways to detect if player is grounded or not. If raycast, SphereCast and other ray based detection API are not working properly, try something different. Use the callback functions such as OnCollisionEnter and OnCollisionExit with flag. Check that flag in the Update function.



          bool m_IsGrounded;

          void OnCollisionEnter(Collision collision)
          {
          if (collision.collider.CompareTag("Ground"))
          m_IsGrounded = true;
          }

          void OnCollisionExit(Collision collision)
          {
          if (collision.collider.CompareTag("Ground"))
          m_IsGrounded = false;
          }

          void Update()
          {
          if (m_IsGrounded)
          {
          Debug.Log("Grounded");
          }
          }


          Note that this is checking for the "Ground" tag so your ground object must be on the "Ground" tag. You have to manually create this tag from the Editor then change your ground object tag to this.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 19 '18 at 17:34









          ProgrammerProgrammer

          77k1089158




          77k1089158













          • I tried that, spent the day on adapting rest of my code but it now works. Thanks.

            – Studio Pie
            Nov 20 '18 at 18:05



















          • I tried that, spent the day on adapting rest of my code but it now works. Thanks.

            – Studio Pie
            Nov 20 '18 at 18:05

















          I tried that, spent the day on adapting rest of my code but it now works. Thanks.

          – Studio Pie
          Nov 20 '18 at 18:05





          I tried that, spent the day on adapting rest of my code but it now works. Thanks.

          – Studio Pie
          Nov 20 '18 at 18:05




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53377778%2fdetect-if-the-player-is-landed-on-top-of-a-box-with-rigidbody%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini