• 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 Yofurioso · Dec 29, 2013 at 11:58 PM · rigidbodyjitterslope

Character Jitter on Slopes

It's my first time using a rigidbody for a character and everything is working fine, except that, when the character goes up or down a slope it jitters (a problem I never had with the character controller and the CharacterController.Move function). Any idea is welcome. I've already tried applying a small upwards/downwards movement when walking o a slope using Mathf.Tan(Vector3.Angle(contact.normal, character.up)) or something like that to get the exact amount of offset movement, it didn't worked.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Pecek · Dec 30, 2013 at 04:38 AM

I assume you are using rigidbody.position for your movement, that's why you are getting the jittering. Use rigidbody.moveposition instead, it's designed especially for this. You may get jittering when you move down on a slope though, but that can be avoided by some tweaking.

Comment
Add comment · Show 3 · 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 Yofurioso · Dec 30, 2013 at 02:54 PM 0
Share

I was actually using transform.Translate (I tried using rigidbody.AddForce but I wasn't happy with the result). I tried using rigidbody.moveposition and I kid you not, its even more jittery than transform.Translate (at least going down a slope, when going up they are pretty much the same). And now Unity refuses to calculate tangent correctly, it gives me values between -0.85 and 6.4 for angles 15º and 30º which is just ridiculous.

avatar image Pecek · Dec 30, 2013 at 07:14 PM 0
Share

Thats strange.. may I see the code snippet? I'm using this, and works fine

 rigidbody.$$anonymous$$ovePosition((rigidbody.position + (direction * speed * Time.deltaTime)))
avatar image Yofurioso · Dec 31, 2013 at 11:05 AM 0
Share

This is the main part of the controller script:

 private void Locomotion()

     {

         if(playerInput.X != 0 || playerInput.Z != 0)

         {

 desiredRotation = CalculateRotation();

             direction = new Vector3(0, slopeCorrection, 1).normalized;

 

             Debug.DrawRay(transform.position, transform.TransformDirection(direction) * 3f, Color.blue);

 

             desiredRotation.z = 0;

             desiredRotation.x = 0;

             transform.rotation = Quaternion.Lerp(transform.rotation, desiredRotation, rotationDamping * Time.deltaTime);

     

             currentSpeed = $$anonymous$$athf.SmoothStep(currentSpeed, maxSpeed, acceleration * Time.deltaTime);

             transform.Translate(direction * currentSpeed * Time.deltaTime);

 //            rigidbody.$$anonymous$$ovePosition(rigidbody.position + direction * currentSpeed * Time.deltaTime);

 

             animator.SetFloat("CurrentSpeed", currentSpeed);

             animator.SetBool("$$anonymous$$oving", true);

         }

         else

         {

             currentSpeed = $$anonymous$$athf.SmoothStep(currentSpeed, 0, locomotionDrag * Time.deltaTime);

 

             transform.Translate(direction * currentSpeed * Time.deltaTime);

 

 //            rigidbody.$$anonymous$$ovePosition(rigidbody.position + direction * currentSpeed * Time.deltaTime);

 

             animator.SetFloat("CurrentSpeed", currentSpeed);

             animator.SetBool("$$anonymous$$oving", false);

         }

     }





Is using "slopeCorrection" to offset for the angle of the slope. Here is the code that has the $$anonymous$$athf.Tan problem

 //$$anonymous$$ore code...
 
                 Physics.Raycast(transform.position, - transform.up, out groundInfo, 2f);
                 angle = (Vector3.Angle(Vector3.up, groundInfo.normal));
                 tan = $$anonymous$$athf.Tan(angle);
                 Debug.DrawRay(groundInfo.point, groundInfo.normal * 10f, Color.magenta);
 
 //$$anonymous$$ore code...

The variables "angle" and "tan" are public float (so that I can see them in the inspector), I wrote this part of the code just as a test so none of the variables are actually used in the script.

avatar image
0

Answer by gfoot · Jan 13, 2014 at 11:28 AM

If you're using rigid bodies then you really ought to be using forces, or at least direct velocity changes, instead of directly manipulating positions. You pretty much throw away all the purpose of a rigid body otherwise.

It may help to apply forces (or position changes, if you must) exactly tangential to the ground plane. I can't see "slopeCorrection" being set in your code so I don't know whether you're calculating it correctly or not. I wouldn't use angles for this - your raycast gives you a ground normal, and you can project your character's forward vector onto the ground plane fairly easily:

 Vector3 groundForward = (transform.forward - groundUp * Vector3.Dot(transform.forward, groundUp)).normalized;

In any case though, if you use forces it should be more tolerant to being moved not exactly along the ground, which is obviously important for dealing with bumpy terrain.

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.

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

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

Prevent Rigidbody from Climbing Steep Slopes using Forces (C#) 0 Answers

Jittery Rigidbody 4 Answers

How to get transform.Translate to work with rigidbodies 2 Answers

Weird jitter on Android using Character Controller 0 Answers

How to move a kinematic rigidbody smoothly? 3 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