- Home /
Jittery Movement
Hi everyone!
I have a "hover craft" that works almost as desired, except its a bit jittery :( When you go straight and slow down it's okay, but as soon as you turn and slow down the background looks jittery even at 600 fps+...
Here's my code so far.
var desiredHeight = 1.0;
var spring = 100.0;
var damp = 10.0;
var SpeedMovement = 5.0;
var turnSpeed = 5.0;
var movDir : Vector3 = Vector3.zero;
var rotDir : Vector3 = Vector3.zero;
var attachedRigidbody : Rigidbody;
function Update() {
if(rigidbody.angularVelocity.magnitude > 2)
rigidbody.angularVelocity = rigidbody.angularVelocity.normalized * 2;
}
//HoverCraft Hovering
function FixedUpdate() {
var cam : Transform = Camera.main.transform;
var hit : RaycastHit;
var up = Vector3.up;
if (Physics.Raycast(transform.position, -up, hit, 100.0)) {
// Force required to negate gravity
var neutralForce = rigidbody.mass * Physics.gravity * 0.2;
var yDifference = (hit.point.y + desiredHeight) - transform.position.y;
var addForce = yDifference * spring;
addForce -= rigidbody.GetPointVelocity(transform.position).y * damp;
rigidbody.AddForceAtPosition(neutralForce + addForce*up, transform.position);
rotDir.y = Input.GetAxis("Horizontal") * turnSpeed;
movDir.z = Input.GetAxis("Vertical") * SpeedMovement;
// Calculate the x-axis relative to the camera
var cameraRelative : Vector3 = cam.TransformDirection (movDir);
// Apply a force relative to the camera's axis
rigidbody.AddForce (cameraRelative);
rigidbody.AddTorque (rotDir);
//transform.rotation = Quaternion.Euler(cameraRelativeRight);
}
Debug.Log(rigidbody.angularVelocity.magnitude);
}
Thanks,
Theokie
It also does this when im moving, not just slowing down :(
Answer by DanMarionette · Aug 21, 2011 at 02:54 PM
I think this may be the same thing I ran into once. I found I was having this problem when I had a camera following a moving rigidbody. I had two scripts, one on the rigidbody and one on the camera. I found that I was having trouble because I was using FixedUpdate() to apply a force to the rigidbody. It is odd because I know that is what your supposed to use but as soon as I moved it into Update() it smoothed it all out. I tried having the camera static and, using FixedUpdate() the rigidbody does move smoothly. It only stutters when you are updating a cameras position based upon the rigidbody's position.
So the first thing I'd try here is to move your FixedUpdate() code into Update(). I know this goes against what you should do but, I would just give it a try first to see what happens. If anyone else knows what is actually going on here I would be interested to know also.
On another note I noticed you are also looking up the cameras transform in every FixedUpdate(). You should just look it up once in Awake() and store it in your cam variable.
I ran into this problem to - just to clarify, make sure your camera movement and your player movement commands are both in the same method, i.e. both in FixedUpdate (ideally), or both in Update().
Answer by aldonaletto · Aug 21, 2011 at 03:42 PM
I tried this script and found no jittering. I childed the camera to the hovercraft and set its position to (0, 2, -10), what placed it above and behind, and had no trouble. I added MouseLook to it and again everything gone right.
Are you using some camera script? If so, maybe it's a problem of Update() x FixedUpdate() timing, as @DanMarionette suspected - but I think you will have more problems if all your code is moved to Update, because you need a stable sustain force. Maybe the other way around is better: move the camera script (if any) to FixedUpdate.
Your answer
![](https://koobas.hobune.stream/wayback/20220612185036im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Hover Effect with more rays as turbines? 1 Answer
Hovercraft Physics Problems 1 Answer
Skateboard slope problem 0 Answers
Physics.Raycast (Cheapest Methods) 0 Answers
Hover spring in local gravity 0 Answers