• 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 huaffinity · Jun 13, 2017 at 05:23 AM · character controller

CharacterController vertical velocity is flickering on ground

So I've got

 private CharacterController controller;
 private float gravity = -100f;
 [HideInInspector]
 public float walkSpeed = 20;
 [HideInInspector]
 public float runSpeed = 40;
 [HideInInspector]
 public float curSpd;
 [Range(0, 1)]
 public float airControlPercent;
 private float speedSmoothTime = 0.1f;
 private float speedSmoothVelocity;
     
 void Start()
 {
     controller = GetComponent<CharacterController>();
 }

 void Move()
 {
     if (!stallMovement)
     {
         if (controller.isGrounded)
         {
             if (!maxSlope && !normSlope)
             {
                 velocity = GroundVelocity(ref velocity);

             if (Input.GetKey(KeyCode.Space))
             {
                 Jump();
             }
         }
         if (maxSlope)
         {
             velocity = MaxSlopeVelocity(ref velocity);
         }
         if (normSlope)
         {
             velocity = NormSlopeVelocity(ref velocity);
             if (Input.GetKey(KeyCode.Space))
             {
                 Jump();
                 canJump = false;
             }
         }
     }
     if (wallSliding)
     {
         WallSlideVelocity(ref velocity);
     }

     controller.Move(velocity * Time.deltaTime); 
 }
 FaceDir();
 }

As my CharacterController's move method controlling numerous movement behaviors in Update(). And

 void ApplyGravity()
 {
     if ((controller.collisionFlags & CollisionFlags.Below) == 0 || maxSlope || !wallSliding)
     {
         velocity.y += gravity * Time.deltaTime;
     }
     
 else
     velocity.y = 0;
 }

is being applied also via Update(). and

 Vector3 GroundVelocity(ref Vector3 vel)
 {
     bool running = Input.GetKey(KeyCode.LeftShift) && controller.isGrounded;
     Vector2 input = new Vector2(Input.GetAxis("Horizontal"), 0);
     Vector2 inputDir = input.normalized;
     float targetSpeed = ((running) ? runSpeed : walkSpeed) * inputDir.magnitude;
     curSpd = Mathf.SmoothDamp(curSpd, targetSpeed, ref speedSmoothVelocity, GetModifiedSmoothTime(speedSmoothTime * 3));
     animSpdPer = Mathf.Lerp(0, 1, curSpd);
     vel = new Vector3(curSpd, 0, 0);

     if (Input.GetKey(KeyCode.D))
     {
         return vel;
     }
     if (Input.GetKey(KeyCode.A))
     {
         return -vel;
     }

     return Vector3.zero;
 }

is what feeds the motion into it on ground.

The end result I'm trying to get is to be able to track velocity (Vector3) to flag some movement states, but I cannot do so since velocity.y is not constantly canceled out to 0 when standing still on the ground. It flickers to some negative amount of velocity.y and then back to 0 constantly. I assume the gravity is majorly the cause of it, but I cannot pinpoint where and why.

I've got part of script not the whole attached here as you can see. I'm sorry for it, but the intention was only for better readability since I was pretty sure nothing else was really causing my issue. Is the velocity.y flicker because of me or just some weird CharacterController issue I'm not aware of? As far as I know CharacterController's ground check via OnControllerColliderHit() / isGrounded / CollisionFlags are all wonky and not truly reliable... Such a pain...

Comment
Add comment · Show 7
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 hexagonius · Jun 13, 2017 at 05:43 AM 0
Share

The CharacterController needs a constant down force to work. Set gravity.y to 0 and it starts floating at some point. If the gravity is 0 or not could be the isGrounded state of it.

avatar image godfreyidk hexagonius · Aug 03, 2018 at 07:06 AM 0
Share

Wow, i would never have guessed that. Thank you for the knowledge dump!

avatar image huaffinity · Jun 13, 2017 at 05:59 AM 0
Share

How would one differentiate from falling to standing, then? In my case, I've got slope velocity separately so I do not need the Y value be changing when there's no movement. I'm doing this on flat ground first, but I think on a slope it'll only get more complicated if the Y value is not constant... Any tips..?

avatar image hexagonius huaffinity · Jun 13, 2017 at 10:30 AM 0
Share

Well falling is not being grounded, isn't it?

avatar image huaffinity hexagonius · Jun 13, 2017 at 10:41 AM 0
Share

And for CharacterController isGrounded itself is not consistent atm for me with script above: the original and sole issue :(

avatar image FunIsDangerous · Jun 13, 2017 at 10:53 AM 0
Share

In the code you are providing you are never calling ApplyGravity, is this a fail on your part, or is there more code?

avatar image huaffinity FunIsDangerous · Jun 13, 2017 at 11:39 AM 0
Share

Sorry for the confusion. As I mentioned ApplyGravity() is being called in Update() and this is part of my player script and those only affecting velocity summarized.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

68 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

Related Questions

CharacterController 2D Move called on inactive controller but it is active... 0 Answers

Switch movement controlls from one object to another 0 Answers

in my endless runner game, when i build apk my character glitches..!! does anyone has the solution 0 Answers

CharecterController.Move() ignores parents movement 0 Answers

3D RPG - Player movement - He automatically finds the fastest way and goes around everything. How to limit his movement? 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