Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Jsim · Feb 14, 2015 at 09:57 AM · rotationraycastgravitygrounded

Detect if on ground?

I need to detect if the player is on the ground... Currently im using this

 Physics.Raycast(transform.position, -transform.up, out hit, yBounds + 0.03f);

which does its job, however it only calculates from the y axis, which if the player is even slightly off center, or theres a crack in the ground, the player's gravity starts pulling him down causing very strange behavior. I attempted calculating from 5 axis using this:

 RaycastHit hit;
         Physics.Raycast(transform.position, -transform.up, out hit, yBounds + 0.03f);
         if(hit.transform != null){
             Debug.Log(hit.transform.name + " On Gound");
             return true;
         }else{
             RaycastHit[] hits = new RaycastHit[4];
             Vector3[] check = {getBounds(xBounds, 0, 0), getBounds(-xBounds, 0, 0), getBounds(0, 0, zBounds), getBounds(0, 0, -zBounds)};
             for(int i = 0; i < 4; i++){
                 Physics.Raycast(check[i], -transform.up, out hits[i], yBounds + 0.03f);
                 if(hits[i].transform != null){
                     Debug.Log(hits[i].transform.name + " On Gound");
                     return true;
                 }
             }
             Debug.Log("OFF - Gound");
             return false;
         }RaycastHit hit;
         Physics.Raycast(transform.position, -transform.up, out hit, yBounds + 0.03f);
         if(hit.transform != null){
             Debug.Log(hit.transform.name + " On Gound");
             return true;
         }else{
             RaycastHit[] hits = new RaycastHit[4];
             Vector3[] check = {getBounds(xBounds, 0, 0), getBounds(-xBounds, 0, 0), getBounds(0, 0, zBounds), getBounds(0, 0, -zBounds)};
             for(int i = 0; i < 4; i++){
                 Physics.Raycast(check[i], -transform.up, out hits[i], yBounds + 0.03f);
                 if(hits[i].transform != null){
                     Debug.Log(hits[i].transform.name + " On Gound");
                     return true;
                 }
             }
             Debug.Log("OFF - Gound");
             return false;
         }


(the getGounds method just returns a vector of the player's position relative to its collider extents:

 public Vector3 getBounds(float x, float y, float z){
         return new Vector3(transform.position.x + x, transform.position.y + y, transform.position.z + z);
     }

)

While this works when the player is not rotated, but once the player is rotated the collider extents are no longer accurate - and in my game the player is constantly being rotated in many directions.

I have no clue what to do, because raycasting beneath the player in this manner is very important to other features of my game. Please help. Can someone also advise me as to whether or not the way I'm doing it now is efficient at all? Would this hurt performance if I did something similar to this?

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 meat5000 ♦ · Feb 14, 2015 at 10:11 AM 0
Share

Use a SphereCast or something that is fatter than a raycast. This way you will avoid the cracks.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by hexagonius · Feb 14, 2015 at 11:00 AM

An easier solution would be to attach a trigger collider to its feet and set grounded according to OnTriggerEnter and Exit

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 Jsim · Feb 14, 2015 at 05:02 PM 0
Share

Is that reliable? $$anonymous$$e and my stubborn self like to do everything via program$$anonymous$$g, but if that will work as often as I need it to, I'm ok with that. But like I said, I need to be able to use this method for more than checking for the ground, like finding the average of several normals

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to get Ray rotation from direction 0 Answers

Get raycast to ignore rotational axis 2 Answers

make projectiles always hit target 1 Answer

Raycasting from object rotation? 1 Answer

click to move workig script!! but pls help with rotation!! :( 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