- Home /
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.
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.
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.
Thats strange.. may I see the code snippet? I'm using this, and works fine
rigidbody.$$anonymous$$ovePosition((rigidbody.position + (direction * speed * Time.deltaTime)))
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.
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613125119im_/https://answers.unity.com/themes/thub/images/avi.jpg)