# Rigidbody Jitter

Hello,

My ship rotates around a spherical object and has a rigidbody attached. It’ll bounce and turn nicely if i hit a collider from an angle. But if the angle is straight onto the collider it will start jittering and moving up trying to solve the collision. What’s a neat way of trying to mask or avoid this from happening?

``````void FixedUpdate()
{
rigidbody.position += transform.forward * Time.deltaTime * ForwardSpeed;

Vector3 surfaceUp = rigidbody.position - Vector3.zero;
Quaternion surfaceUpRotation = rigidbody.rotation;
surfaceUpRotation.SetLookRotation(surfaceUp, -transform.forward);
surfaceUpRotation *= Quaternion.Euler(90,0,0);
rigidbody.rotation = surfaceUpRotation;

if( Input.GetKey(KeyCode.D) )
{
rigidbody.rotation *= Quaternion.Euler(0, RotateSpeed * Time.deltaTime,0);
}
else if( Input.GetKey(KeyCode.A) )
{
//transform.Rotate(transform.up, -RotateSpeed * Time.deltaTime, Space.Self);
rigidbody.rotation *= Quaternion.Euler(0, -RotateSpeed * Time.deltaTime,0);
}
}
``````

Have you tried to avoid using rigidbody.position and rigidbody.rotation? You need to read the documentation of those two properties to understand why you probably should not use them for your case.

I understand why, i just cant seem to get the same movement that i have with the current code using MovePosition and it’s ilk.

Another option would be to apply forces for the forward movement instead of setting it directly. The same for the rotation, you may try to apply torque.

I made it work with MoveDirection and MoveRotation but it’s causing a lot of jitter now on the ship but it’s stopped it from moving upwards during collision.

Are you already using interpolation for the rigidbody? It sometimes helps to reduce jittering.

I was adding a force downwards as well, i removed that and alot of the jitter went away. doesn’t stop it from moving up anymore however.

What do you mean with interpolation?

I mean the rigidbodies interpolation method. It is None by default. You may try Interpolate or Extrapolate to see if that helps.

Here’s a screencast of the jitter behavior: http://www.youtube.com/watch?v=E11FNiUFQjI

The interpolation doesn’t do much i’m afraid.

From my point of view the jittering is not that bad. It sums up as the camera has a “static” position relative to the ship.

Two ideas come to my mind:

• Try to smooth the jittering for the camera
• Freeze the rotation of the rigidbody and set the rotation as you did it before. That may produce strange results due to intersection with the environment, but it maybe it reduces the jittering.

The ship does shoot up into the sky. Wouldn’t it be a better idea to stop the forward motion on collision and then check if there is something ahead untill we move forward again? Or something in that vein?

Definitely yes! As long as you collide with the environment in front of you, don’t apply the forward movement. Maybe if you are close which you can detect with a trigger, only use a reduced speed or something like that.

still getting very odd behavior where my ship floats off into the air or starts steering to the left or the right on collision. What are the benefits of the rigidbody anyway at this point? Other then the OnCollisionEnter events? Am i not better off just raycasting all the time and managing the events myself?

I mean, the movement has to be have an arcade feel to it. If i use AddForce and Torque the movement becomes pretty floaty and loses the arcade feel.

You may get the arcade feel back if you heavily increase the drag and angular drag values. It is a matter of trying out which one works.

It is indeed questionable if you need rigidbodies. Instead of adjusting the physical behaviour slightly where necessary, you anyways set all the positions “manually” :).

I would only need to start writing my own collision detection which is a pain

I know this thread is super old, but I thought I’d share my 2 cents for those who find it in the future.

I was struggling with a very similar issue. The solution I found was to use the different “OnCollision” events to detect the start of a collision, while also checking for changes in “Input” direction. My motion controller was using this formula

``````var move = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0);
transform.position += move * motionDistance * Time.deltaTime;
``````

So then I added checks inside `OnCollisionEnter2D` and `OnCollisionStay2D`(or the 3D versions for your own code) to set the last key pressed when the collision began and then check the current key pressed while the collision is still occurring. If those two values change while `OnCollisionStay2D` is still in effect, then I allow the motion transform to resume. Otherwise I block it from happening entirely.

``````    void OnCollisionEnter2D( Collision2D collision ){
lastKeyPressed = setCurrentKeyPressed ();
isNotColliding = false;
}
void OnCollisionStay2D( Collision2D collision ){
setCurrentKeyPressed ();
directionHasChanged = lastKeyPressed != currentKeyDown;
isNotColliding = directionHasChanged;
}
``````
``````    private string setCurrentKeyPressed(){
if (Input.GetKey ("left")) {
currentKeyDown = "left";
}
if (Input.GetKey ("right")) {
currentKeyDown = "right";
}
if (Input.GetKey ("up")) {
currentKeyDown = "up";
}
if (Input.GetKey ("down")) {
currentKeyDown = "down";
}

return currentKeyDown;
}
``````

And so now I have a guard to check for applying the transform

``````        if (isNotColliding) {
transform.position += move * motionDistance * Time.deltaTime;
}
``````