Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by oinkoinkflapflap · Jul 28, 2011 at 06:38 PM · rigidbodycheckgroundedfpswalker

checking if grounded on rigidbody

i've looked through unityAnswers and not found what i'm looking for... like on the FPSWalker script, i need a way to find whether a rigidbody is grounded or not through java...

thanks for your help...

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Chris D · Jul 28, 2011 at 06:45 PM 1
Share

have you checked out the version for use with rigidbodies?

http://www.unifycommunity.com/wiki/index.php?title=RigidbodyFPSWalker

5 Replies

· Add your reply
  • Sort: 
avatar image
9

Answer by 1elfdragon1 · Apr 30, 2013 at 05:59 PM

this is in C#

 public bool IsGrounded;
 
 void OnCollisionStay (Collision collisionInfo)
 {
      IsGrounded = true;
 }
 
 void OnCollisionExit (Collision collisionInfo)
 {
      IsGrounded = false;
 }

this is in Js:

 private var IsGrounded = false;
 
 function OnCollisionStay (Collision collisionInfo)
 {
     IsGrounded = true;    
 }
 function OnCollisionExit (Collision collisionInfo)
 {
     IsGrounded = false;    
 }

edit: currently i use this: C#

 float GroundDistance;
 bool IsGrounded ()
 {
     return Physics.Raycast (transform.position, - Vector3.up, GroundDistance + 0.1f);
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RealSoftGames · Mar 09, 2015 at 09:22 PM 0
Share

this is in C#

public bool IsGrounded;

void OnCollisionStay (Collision collisionInfo) { IsGrounded = true; }

void OnCollisionExit (Collision collisionInfo) { IsGrounded = false; }

although this is perfect for what i want, is there a way to make it only work on the y axis? e.g for a RPG game, i don't want players running at a wall spam$$anonymous$$g the Jump key. its as easy as adding a time between jump, but it will still be possible to give unpredictable results from players.

and thank you for sharing this. that method is what i was after, wont be to hard to modify it though.

avatar image
1

Answer by FLASHDENMARK · Jul 28, 2011 at 07:00 PM

There are several ways for you to detect wheater you are on the ground or not.

SphereCast is generally a good idea.

OnCollisionEnter Can also be used.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by Pendantic · Mar 09, 2015 at 09:41 PM

I've typically done something like this

         public float isGroundedRayLength = 0.1f;
 public LayerMask layerMaskForGrounded;
 public bool isGrounded {
                 get {
                         Vector3 position = transform.position;
                         position.y = GetComponent<Collider2D>().bounds.min.y + 0.1f;
                         float length = isGroundedRayLength + 0.1f;
                         Debug.DrawRay (position, Vector3.down * length);
                         bool grounded = Physics2D.Raycast (position, Vector3.down, length, layerMaskForGrounded.value);
                         return grounded;
                 }
         }

This specific example uses 2D physic but just take out the 2D for 3D games. Using the layer mask allows you to ignore certain object like other players or the player themselves. This also uses the colliders lowest point for the y so no matter how tall your object is this will work.

The only issue with this code is if you have a rather wide character and you move onto an edge where your centre is no longer above the ground but the sides are. If this does become an issue, either use sphereCast or boxCast as they will more accurately cover that area, but are more expensive if just a single ray will do.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by MrReynevan2 · Feb 01, 2013 at 01:21 AM

But what when I for example have racing game? The map is full of small pieces. I've tried to make variable "IsGrounded" which changes state thanks OnCollisionEnter and Exit, but it wasn't great idea. When car collides new map element, the variable state is changed to true, but sec later it returns to 0 because last element is lossing collision. What can I do in this situation?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by aaronov · May 09, 2013 at 11:16 AM

Another option but it might not be the most efficient is perform a few Raycasts down to the ground and check if they hit anything. I've found this more reliable in some situations but again, it could be inefficient if you're doing lots of them from lots of objects.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rigidbody jump script collision help. 1 Answer

Is there a way to explicitly check for collision? 1 Answer

Colliders Not Working 1 Answer

How can I check if the ragdoll is grounded? 0 Answers

Check if Rigidbody collider is grounded 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges