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 JSmithsonian · Feb 17, 2016 at 09:42 PM · rigidbodyraycastisgrounded

How to check if rigidbody is grounded with raycasts on uneven surfaces?

I am currently checking if I am on ground with a short raycast from the transform.position of my character. This works fine on even surfaces and even while going up slopes. But it doesn't work at all when you try to go down a slope. The character constantly goes midair when I try to go down a slope.How can I solve this issue.This is the code that is being used currently:

 bool Grounded()
     {
         return Physics.Raycast(transform.position, Vector3.down, moveSettings.distToGround, moveSettings.ground);
     }


Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by hyagogow · Mar 23, 2017 at 12:28 PM

Use a BoxCast and set its size to your collider size.

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 toddisarockstar · Mar 23, 2017 at 04:39 PM

I use this script. it works magically. it simply shows grounded if your guy is touching anything with a name containing "floor". if you dont want to rename you terrain i added an alterative code at the bottom to check if the contact point is below a certain height on the collider to detect instead.

     int i;
     int touch;
     int ot;
     int pad;
     bool yes;
     bool sw;
     GameObject p;
 
     public bool grounded;
     public int framepad;
 
     //the frame pad variable is the amount of frames delay before "grounded" is set back to false
     //incase your character slightly bounces or something
 
 
 
     void Update () {
         if (touch != ot) {if(touch>20){touch=0;}
             ot=touch;pad=1+framepad;}
         else{if(pad>0){pad--;}}
         
 
         if(pad>0){grounded=true;}else{grounded=false;}
     
 
     }
 
 void OnCollisionStay(Collision c) {
 
         //i reccomend just naming your terrain to floor    
 
         if (c.transform.name.Contains("floor")) {touch++;}
 
 
         // alteratively you dont want to name your 
         // floors, you could do it like below instead to see if the contact point 
         // is below a certain height on your character hopefully the bottom of you guy is round
         // otherwhise it will trigger walls. you could of course put this script on a seperate collider for 
         // your feet:)
 
         bool b = false;
         i = c.contacts.Length;
         while(i>0){i--;    
             //fine tune the nubmer in the next line 
             //it sets the up/down cuttoff for where your feet are on the collider!!!
         if(c.contacts[i].point.y<transform.position.y+.01f){b=true;}
         }
         if (b) {touch++;}
         }
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Gravity makes player slide on any slope!!! Check if rigidbody Isgrounded not working 0 Answers

How to stop waypoints script in another script? 0 Answers

How to access component that the raycast hit 1 Answer

How to detect ground slope angles in 3d? 1 Answer

Boolean doesn't change at start position. 1 Answer


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