- Home /
Rigidbody jerky movement?
Hello all, first-time unity user and poster.
I'm working on a basic control system to fly a 3rd person spaceship around in 3D space using physics. I have a script which simply gets user input for positive or negative thrust, then multiples that by the direction and an acceleration multiplier. There's a bunch of other code, but slowly debugging by turning things off showed that neither mouselook aiming nor top speed limiting changed anything.
> forceToAdd = Vector3.forward *
> CheckForThrust()*accelMult;
> rb.AddRelativeForce (forceToAdd);
Checkforthrust:
> function CheckForThrust(){
> // If accelerate key is pressed, set accel to increase to 1. if
> backwards, interpolate to -1. If
> nothing, head to 0
>
> if (Input.GetKey("q")) {
> engineStatus = Mathf.Lerp(engineStatus,1,0.01);
> } else if (Input.GetKey("e")) {
> engineStatus = Mathf.Lerp(engineStatus,-1,0.1);
> } else {
> engineStatus = Mathf.Lerp(engineStatus,0,0.01);
> }
>
> Mathf.Clamp(engineStatus,-1,1);
> return engineStatus;
> }
When acceleration starts, everything's fine. But when I get past a certain velocity threshold the ship starts jittering like mad. It's not related to application of force, because if I ease up on the thrust and stop accelerating, the jittering remains. I tried setting the rigidbody to Interpolate and Extrapolate. Extrapolate seemed to improve things a bit, but not much. I even tried applying the force inside Update() instead of FixedUpdate() even though you're not supposed to, that just made it worse.
Any ideas?
Are you using some sort of 'follow camera' script by any chance?
Need more information. Did you try and change the function? Are you debug logging values to see if there's a pattern of differences? Did you remove the function and just test raw acceleration? Is the problem in the input code or somewhere else in the rigidbody code?
I'm using a camera following script, yes. it still occurred if I disable the script.
I did figure it out though! what was happening was that force was being applied between each solver step due to the input code being badly designed. redesigning the input and slightly increasing solver steps fixed the problem