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 TheYokai · Feb 12, 2014 at 11:10 AM · spherecastground detection

Checking if grounded using SphereCast -- Acting like Raycast

So I've been trying to convert my old Physics.Raycast grounding check to a Physics.spherecast for the sake of being more lenient with what is considered "grounded" in my game. Unfortunately my new spherecast check is working exactly the same as my old Raycast check and is causing this problem below:

alt text

Basically, the issue is that my game requires the player to be grounded in order to perform a jump (which is pretty standard) and uses the IsGrounded function to detect if the player is on the ground. The function looks like this:

 public bool IsGrounded()
     {
         RaycastHit rayHit;
         float characterHeight = GetComponent<BoxCollider> ().size.y;
         float characterWidth = GetComponent<BoxCollider> ().size.x;
         Vector3 startPoint = transform.position;
         return Physics.SphereCast (startPoint, characterWidth/2, Vector3.down, out rayHit, characterHeight / 2);  
     }

The issue is that I want the player to be considered grounded even when he/she is barely hanging off the edge as the image above to allow for precise jump timing and to make sure that there's no chance of the player's jump request to be ignored because the pawn class' internal physics state is set to airborne.

Maybe I'm doing this entirely the wrong way, but it seemed like a SphereCast would be the ideal solution to having a volumetric raycast / quick collision detection. I could also do multiple raycasts, but that probably wouldn't be optimized. Any reason why this current method isn't work or what else I should try to get it working?

testimage.png (193.4 kB)
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 Scribe · Feb 12, 2014 at 11:31 AM 1
Share

You should take a look at how isGrounded works in the character controller standard asset. I'm not certain but I think they use OnCollisionEnter() and check if the y value of the collision points is < 0 (meaning that the player is colliding with something below its centre, you might change 0 to < 0.9 depending on the size of your collider) if a collision point matches the less-than then they set isGrounded to true, then set it to false in OnCollisionExit()

Scribe

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by whydoidoit · Feb 12, 2014 at 11:28 AM

Sphere casting might be more expensive compared to multiple ray casts - especially as multiple casts can cease as soon as any of them finds the ground. You should really be caching that BoxCollider GetComponent call too - indeed presuming the size doesn't change you could just cache that in Start.

I'm unsure of why your sphere cast isn't working unless it's something to do with getting the wrong distance etc (is your model's position in the centre? It appears to be, in which case the sphere will be very narrow when it reaches the ground if you are casting exactly that far).

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

20 People are following this question.

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

Related Questions

Spherecast for ground check glitch 1 Answer

Is it possible to capture a 'free' parent transform, make it into a child, and then move it with the parent? 0 Answers

SphereCastAll sees for miles 2 Answers

Physics.SphereCast to detect things below my character? 0 Answers

Why won't my spherecast work? 0 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