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
1
Question by toum · Oct 22, 2011 at 07:27 PM · charactercontrollergroundedslide

Grounded state problems when sliding

Dear all,

I got a problem with grounded state. I'am using a charater controller with the "isGrounded" feature.

In my game the controller have to slide along a slope (slope can down and up).

= > When i am in "air" i apply a gravity value. When grounded gravity.y = 0 (Lerped from -9.8 to 0 to avoid bouncyness on character controller).

The problem is that my grounded state swing between true and false even if visually the character controller is sliding on floor.

So character controller can't jump. I tried to use raycast instead of "isGrounded"'s character controller method. It's quite better for controlling the grounded state.

Here is my script with raycast:

 public class Capsule_3 : MonoBehaviour {
 protected Vector3 _gravity     = Vector3.zero;//-9.8f /3;
 protected Vector3 _dir         = Vector3.zero;
 protected Vector3 _jump     = Vector3.zero;
 protected Vector3 GRAVITY     = new Vector3(0, -9.81f, 0);
 protected Vector3 velocity     = Vector3.zero;
 protected CollisionFlags collisionFlags;
 protected CharacterController controller;
 protected RaycastHit     hit;
 protected float amountOfMotion = 0.0f;
 protected bool grounded;
 
 void Awake () {
     // Getting CharacterController
     controller = GetComponent<CharacterController>();
 }

 void Start () {
 }
 void Update () {

     InputListener ();
     handleDirection ();
     handleJump ();
     handleGravity ();
     Vector3 vectorSum =     _gravity +
                              _dir + 
                             _jump;

     collisionFlags = controller.Move(vectorSum * Time.deltaTime);
 }

 void handleGravity () {
     if (isGrounded()) {
         _gravity = Vector3.SmoothDamp(_gravity, Vector3.zero, ref velocity, 0.5f); // Gradually reduce gravity overtime: avoid character controller boucyness
     } else {
         _gravity = _gravity + GRAVITY * Time.deltaTime;
     }
 }
 void handleDirection () {

     Vector3     rayDir = -Vector3.up * 5;
     float         slopeAngle = 0;
     float         slopeSign = 0;
     Vector3        orthogonalizedNormal = Vector3.zero;
     
     if (Physics.Raycast(transform.position, rayDir, out hit)) {
     
         slopeAngle = (Vector3.Angle(Vector3.up, hit.normal)); // Retreive the slope angle
         slopeSign = Mathf.Sign(hit.normal.z) * -1;
         orthogonalizedNormal =  Quaternion.Euler(90, 0, 0) * hit.normal;

         if(hit.distance > 1.2f) {
             grounded = false;
         } else {
             grounded = true;    
         }
     }

     if(isGrounded()){

         float variationToApplyFromSlope = GRAVITY.y    * Mathf.Cos(slopeAngle * Mathf.Deg2Rad) * slopeSign * Time.deltaTime;
         amountOfMotion = amountOfMotion + variationToApplyFromSlope;
         _dir = orthogonalizedNormal * amountOfMotion; // On prend la direction de l'orthogonale à la surface
     }
     _dir = transform.TransformDirection(_dir); // Convert local to world space

 }

 void handleJump () {
     if(_jump.y > 0 && isGrounded()) {
         _jump = _jump + GRAVITY * Time.deltaTime;
     }
 }

 void InputListener() {
     if (Input.GetKeyUp("space"))
     {
         _jump = Vector3.up * -2.0f * GRAVITY.y;
     }
 }
 
 public bool isGrounded () {
     return grounded;
     //return (collisionFlags & CollisionFlags.CollidedBelow) != 0;

 }

}

As you can see the direction where to go is orthogonalized (90 degree) with the normal of the surface. I have done that to have my character controller sticked on the slope (and avoid bouncyness effect when direction is z+ and y-).

However my character must not stick to the slope when there is a jump. it's easy to do that with character controller (isGrounded function), but with raycast it is not accurate.

My problem is that i need "isGrounded" function from character controller to be able to handle jumps on the slope correctly but this function is not accurate on ground state.

That's the opposite for raycasting...

I need the cons of the two... Or maybe i am on wrong way and there is another solution... :s

Thanks for your support,

Cheers !

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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

I have a issue with Player Grounding. 1 Answer

How should I check if the charactercontroller is grounded next in the next calculation? 1 Answer

Let Character Controller jump. 0 Answers

Let Character Controller jump. 1 Answer

I am looking for a code which makes sure the character is grounded when the game starts,Is there a code to make sure the character is grounded when the game starts? 2 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