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
1
Question by SimonJRogers · Aug 29, 2018 at 04:08 PM · raycastcollidercapsulecast

What is wrong with my Capsule cast?

Hi,

Im trying to get a capsule cast working to iron out some problems with the raycast in my isGrounded method.

Im not really sure I'm doing it right, could anyone tell me where Im going wrong?

Thanks!

 bool isGrounded (){

     bool foundHit;

     RaycastHit hit = new RaycastHit() ;

             //distance of points from centre of capsule.
     float distanceToPoints = capCol.height / 2 - capCol.radius;

             //Position of each end of capsule.
     Vector3 p1 = transform.position + capCol.center + Vector3.up * distanceToPoints;
     Vector3 p2 = transform.position + capCol.center - Vector3.up * distanceToPoints;

     float radius = capCol.radius * 0.95f;
     float castDistance = 0.1f;

     return foundHit = Physics.CapsuleCast (p1, p2, radius, Vector3.down, hit, castDistance);
 
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Ermiq · Aug 30, 2018 at 02:07 AM

Imagine the capsule as two spheres and a cylinder between them.

    ___
  /     \___Sphere2
   \___ _/
  |      |
  |  ___ |
  /     \___Sphere1
  \_____/

For the CapsuleCast function you need center points of these 2 spheres and a radius of any of the spheres. So,

 //get the point which is located at 'capCol.radius' higher than transform's bottom
 Vector3 centerOfSphere1 = transform.position + Vector3.up * capCol.radius;
 //and get th point which is located at
 // ('capCol.height'-'capCol.radius') higher than transform's bottom
 Vector3 centerOfSphere2 = transform.position + Vector3.up * (capCol.height - capCol.radius);

That's how you get positions. Don't ever add floats to a vector, as you did, Unity won't trigger an alarm and will give you some vector as a result but what the result it will be, nobody knows, because vectors don't work like that. You have to add vectors to vectors, not floats.
To get some point position when you know another point (e.g. transform.position) you need to determine in which direction from the transform that point is located. Here we know that the spheres' centers are up from the transform bottom point (transform.position), so we are taking transform.position, then add the direction straight up (Vector3.up), and multiply the direction with a distance float that we need our point to be from the origin. Take transform.position, push it up and specify what the range hight should be. transform.position + Vector3.up * calCol.radius , so Position plus direction multiplied by distance. Like that.
Ok. Next thing I need to tell you. When you cast a capsule for ground detection, you need to set the radius of the capsule you cast a bit less than actual character's capsule. Why? Because Unity uses a special offset to detect collisions, when two colliders are penetrating into each other, Unity let them to do that until the penetration gets higher that this special offset. In that moment Unity detects a collision between these colliders. This offset is called 'Physics.defaultContactOffset' and it's 0.01 by default.
So, if you don't make the casted capsule radius lower by this offset, your character bottom could fall into the terrain for 0.01 units and your capsule won't detect the ground because it will start detecting process while it's already under ground.
So, I suggest you to call a cast method like this:

 RaycastHit hit;
 Physics.CapsuleCast (centerOfSphere1, centerOfSphere2, capCol.radius - Physics.defaultContactOffset, Vector3.down, hit, castDistance);
 if (hit.transform //capsule detected collision with some transform
     && hit.transform != transform) //It's not the character himself was detected
 {
         isGrounded = true;
 }

Oh, and also, you need to set spheres' centers higher by that contact offset as well.

 Vector3 centerOfSphere1 = transform.position + Vector3.up * (capCol.radius + Physics.defaultContactOffset);
 Vector3 centerOfSphere2 = transform.position + Vector3.up * (capCol.height - capCol.radius + Physics.defaultContactOffset);

Good luck with your project.

Comment
Add comment · Show 2 · 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 SimonJRogers · Aug 30, 2018 at 08:44 AM 0
Share

Thanks for your help!

The only thing it wanted other than what you suggested was 'out hit' rather than just 'hit' in the 'Physics.CapsuleCast' line.

Working smoothly now :)

avatar image Ermiq SimonJRogers · Aug 30, 2018 at 08:18 PM 0
Share

By the way, you would better use SphereCast for ground check. It's easier for CPU to calculate just one sphere ins$$anonymous$$d of the capsule.
CapsuleCast is better to use when you need to check if character could move somewhere, for example, if your character has a warp skill, you could cast the capsule forward to check if the character could warp to that place and there's enough space for him. For ground check you just need a lower part of the character, 'Sphere1' from my picture, so it's better to use SphereCast.
And one more thing, if the answer helped you, would you kindly click the 'Accept' button just below the answer?

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

141 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 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 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 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 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 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 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 avatar image

Related Questions

How to block dragging if collider hits 1 Answer

Can't get a laser working properly. 2 Answers

why are child colliders sometimes ignored? 2 Answers

Character Controller can pass through Collider 1 Answer

Destroy objects within ordered Array by line of sight 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