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 Vox Nephila · Jan 26, 2013 at 01:41 PM · c#charactercontrollercontrollergravityisgrounded

controller.Move doesn't stay grounded when walking down slope

 public float gravity = 15.0f;
 
     ...
 
 if (controller.isGrounded) {
 
     ... (this is where all of the movement happens) ...

     moveDirection.y -= gravity * Time.deltaTime;
     controller.Move(moveDirection * Time.deltaTime);

     ...
 
 } else {
     print("ungrounded");
     moveDirection.y -= gravity * Time.deltaTime;
     controller.Move(moveDirection * Time.deltaTime);
 }

When I use this code, I constantly get "ungrounded" popping up in my console whenever I move down any sort of slope. I tried to turn the gravity up to 100, and it fixed it for a 15 degree slope, but it still did it for steeper slopes.

My question is how do I get around this? I want to be able to jump while going down a hill but it's just not possible in the current state since the character is always ungrounded when he is going down a slope.

If you share code, please do it in C#. Thanks.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by schaddem · Jan 26, 2013 at 06:53 PM

Your Problem is that isgrounded returns false, so it's a matter of how isgrounded works, which if the documentation doesn't tell we can only guess at(as far as I know).

I'd imagine that it checks if controller has contact with another collider and then checks the angle. Since touching a wall should normally not qualify as being grounded. Changing the gravity to anything higher than 0 should have no effect at all. Slopelimit might be used for the grounded check, to define what passes as ground and what passes as wall.

1.this might fix it:

 controller.slopeLimit = 90.0;

2.if it doesn't you might want to replace isgrounded with your own function. In which I would do a raycast towards the ground and return true if the distance is small enough.

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 Vox Nephila · Jan 26, 2013 at 07:09 PM 0
Share

Yeah the slope limit doesn't change anything. I'll look into a raycast now. Thanks.

avatar image
0

Answer by Loius · Jan 26, 2013 at 07:07 PM

The way I fixed this was to have a 'default gravity' instead of zero gravity while the character's grounded.

For instance:

 if ( isGrounded ) {
   localGravity = baseGravity;
 } else {
   localGravity -= Time.deltaTime * 9.81;
 }
 
 inputVector.y = localGravity;
 
 controller.Move( inputVector ); 

Then the character will follow slopes downward if a movement amount of (baseGravity) keeps it attached to the slope.

Be aware that this does 'snap' the character down by that amount if they walk off a sheer cliff, so keep it low enough that it's unnoticeable to the player.

Here's the exact snippet I used. Note that the gravity check is to see if the player is jumping ('positive gravity'):

             if ( onGround && curGravity <<= 0 ) {
                 collisionFlags = controller.Move( (targetMovement*modSpeed+Vector3(0,-8,0) ) * Time.deltaTime );
             } else {
                 collisionFlags = controller.Move( (targetMovement*modSpeed+Vector3(0,curGravity,0) ) * Time.deltaTime );
             }
 
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 Loius · Jan 26, 2013 at 07:11 PM 0
Share

I had to use <<= ins$$anonymous$$d of less-than-equals because less-than-equals breaks out of the code and formats down to my name as code -_-

avatar image Vox Nephila · Jan 26, 2013 at 08:05 PM 0
Share

I combined your answer and the other answer and came up with my own collision detection using a raycast. So now I have more control than just the standard "is.Grounded". While the distance to the ground is less than .5, there is a constant force being applied to the actor that keeps him grounded while walking down the slope.

This also allows me to keep falling separate, so when you fall off a ledge, you will move at the regular falling speed, until you get within .5 of the ground of course, which isn't noticeable.

avatar image
0

Answer by schaddemm · Jan 29, 2013 at 09:51 PM

I think it could be pretty helpful to have a look at the charactormotor script (it's in java I think but there's not that much difference in reading the languages). It also has examples of doing stuff like moving on platforms. (the fps controllor script uses the charactermotor).

As far as I understand the script they implement theyre own test:

 private function IsGroundedTest () {
     return (groundNormal.y > 0.01);
 }

groundNormal is the normal of the collider we hit:

 function OnControllerColliderHit (hit : ControllerColliderHit) {
     if (hit.normal.y > 0 && hit.normal.y > groundNormal.y && hit.moveDirection.y < 0) {
         if ((hit.point - movement.lastHitPoint).sqrMagnitude > 0.001 || lastGroundNormal == Vector3.zero)
             groundNormal = hit.normal;
         else
             groundNormal = lastGroundNormal;
         
         movingPlatform.hitPlatform = hit.collider.transform;
         movement.hitPoint = hit.point;
         movement.frameVelocity = Vector3.zero;
     }
 }

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

12 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

Related Questions

Gravity for my AI 0 Answers

How reliable are isGrounded checks? 1 Answer

why does character controller accelerate off ledges? 1 Answer

Round Planets and Movement on them 2 Answers

Making a player controller for a jet fighter 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