Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

rigidbody vibration

Discussion in 'Editor & General Support' started by creat327, Feb 4, 2011.

  1. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,756
    Hi

    I have this fast moving airplane that I move by script. It's pretty simple, I calculate where it should be and I tell the rigidbody to place it there.
    What's odd is that the plane vibrates like crazy. Like it moves back and forth, just a bit, but enough to make it look way too shaky.

    I tried everything, kynematic off, interpolate, extrapolate, none... nothing

    The code is very simple, inside the FixedUpdate:

    Vector3 velocity = currentHeading.normalized * (currentVelocity+(acceleration *Time.deltaTime));
    rigidbody.MovePosition(rigidbody.position+velocity);


    any ideas how to stop it from vibrating? In theory, without gravity and kinematic, it should not move without me telling it to :?
     
    Last edited: Feb 5, 2011
    OfficialHermie likes this.
  2. Vert

    Vert

    Joined:
    Mar 23, 2010
    Posts:
    1,099
    I had a particle emitter attached to the basic FPS controller to simulate snow and it was jittery as well. I unchecked use fixed update and the snow no longer jitters. Perhaps these issues are related?
     
    ahungrybear likes this.
  3. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,756
    nope. it must be some floating precision error.
    I removed the rigidbody and did a simple transform.position=

    It's still vibrating.

    I guess I can somehow use AddForce and that way let the physics engine handle it and avoide the vibration but I can't figure out a way to add the previous example as a force. I tried with AddForce as velocity, acceleration,etc... no luck.
    Any ideas welcome :)
     
    ahungrybear likes this.
  4. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    Don't see the reason to put this code in FixedUpdate, since you just change object's position and nothing more.
     
    ahungrybear likes this.
  5. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,756
    it doesn't matter where i place it, it vibrates still, i can put it on Update too
     
    ahungrybear likes this.
  6. DallonF

    DallonF

    Joined:
    Nov 12, 2009
    Posts:
    620
    The problem may actually be in your camera script. Change it to LateUpdate().
     
  7. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,756
    LateUpdate()... nothing, same thing
    If it was my camera script I guess the whole screen and objects on them would vibrate. Only this gameobject vibrates.

    I know it can be solved by using AddForce or AddRelativeForce, anyone knows how I can implement those simple 2 lines of code into a AddForce to the rigidbody?
     
  8. DallonF

    DallonF

    Joined:
    Nov 12, 2009
    Posts:
    620
    Well, you typically don't want to directly move a rigidbody; rigidbodies like to manage their own movement, and you should let them. Turn kinematic off and set the velocity property of the rigidbody:

    rigidbody.velocity = newVelocity;

    Also, your "velocity" variable in your code is a "float". I don't even know how that compiles, considering that it should be a Vector3.

    One more thing: This is C# code, right?
     
  9. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,756
    that was a typo, I just fixed it. Velocity is a vector3.
    All you said was tried and still vibration continues. These are all the alternatives I've tried:

    1) Update, FixedUpdate, LateUpdate
    2) Rigidbody kinematic, on/off. Interpolation none, interpolate and extrapolate.
    3) Moving the rigidbody with MovePosition, directly with .position or using .velocity.
    4) Moving the rigidbody with AddForce
    5) Ignoring the rigidbody just for testing purposes, removing it completely from the object and just using transform.position

    ALL of the tries, make the object move as expected. Meaning, the object moves at the speed and direction I intended it. But it shakes, it has a very noticiable vibration. The faster it goes, the more it vibrates.

    The other thing then could be my camera the one that it's shaking, but no other object in the scene shakes so that seems to rule it out.

    Joaquin
     
  10. DallonF

    DallonF

    Joined:
    Nov 12, 2009
    Posts:
    620
    Can you post your whole script? It seems to me that you're not giving the whole thing and some critical variables have been left out.
    Post your camera script, too; it may be that it's shaking so little that only the object closest to the camera (i.e. the plane) seems to shake.

    Another thing would be to watch it from the Scene view rather than the Game preview to see if it's really shaking in the world.
     
  11. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,756
    Really, that's just the whole script. The typo was just because I renamed the variables here to make them more clear.
    There is nothing weird, odd or extraordinary about it.

    About the camera, I'm using the smoothfollow script from the standard assets, again, nothing extraordinary.
    Objects that doesn't move, they don't vibrate, it doesn't matter if i get close to them. It's only the objects that move FAST the ones that vibrate. So go to 50km/h it's ok, going 100km/h begins vibration. It's an airplane btw.

    It seems some kind of floating number error. I can't check it since all Vectors are float and there are not double alternatives so unless I implement my own vector3 it would be hard to figure.

    I guess there is no solution for this one then.
     
  12. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,756
    Ok, I figured something new.

    The smoothfollow uses LateUpdate, which makes sense. But, if I change the LateUpdate to FixedUpdate, my moving object looks fine!
    Of course, the object that smoothfollow follows, looks shaky now. That makes sense since it changes position between the calls.

    But I don't get why if I set the SmoothFollow script to FixedUpdate, my fast moving object looks fine. They seem to be synchronized that way. But then my other moving object (which is also in FixedUpdate) that the camera follows, it's shaky.
     
    zxartisxz and finding0625 like this.
  13. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,756
    I've fixed it and it's droven me nuts for days so... for people that might have the same problem in the future, this is the solution and what might have lead you into it.

    If you use the standard smoothfollow script and experience shaking of the object that you follow, the documentation states that you can set the rigidbody of the object to interpolate and it will fix it. This is true, it will fix the shaking of that object, but it will make every moving object in your scene shake when moving at high speeds.

    Solution: Don't set it to interpolate. Leave it as 'none' and move the smoothfollow code from LateUpdate into FixedUpdate.
    Done, no more shaking of your main character and no more shaking of your other objects.

    So simple once you know :)
     
  14. deadfire52

    deadfire52

    Joined:
    Jun 15, 2011
    Posts:
    101
    Thank you very very very much! The 2D follow script is normal Update, changed to FixedUpdate and no more wiggle!
     
    Last edited: Aug 5, 2012
    slumtrimpet likes this.
  15. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    You probably don't need Time.deltaTime either since fixedupdate runs at a fixed rate, and interpolation is basically doing that for you on the rigid body.
     
    Aepeac and Psyco92 like this.
  16. jfabris

    jfabris

    Joined:
    Sep 17, 2011
    Posts:
    12
    I was having the same problem and none of the fixes posed in this thread worked.
    What did work was changing Is Kinematic from unchecked to checked.

    Unfortunately, this prevents my tank from getting knocked around when it is hit by an explosion.

    I am using the Basic Tank Kit from the Asset Store.
    There is no problem when the tank is on terrain, there is only a problem when it is on a plane.

    Here is the code

    function FixedUpdate () {
    tanksVelocity=2;
    transform.Translate(Vector3(0, 0, tanksVelocity * Time.deltaTime));
     
    Last edited: Oct 26, 2011
  17. jfabris

    jfabris

    Joined:
    Sep 17, 2011
    Posts:
    12
    Fix found by Sobhan Bahrami;

    add the Smooth Follow script to the camera.
    I also had to set the height dampening to a high number 100
     
  18. MuffinMyst

    MuffinMyst

    Joined:
    Nov 17, 2014
    Posts:
    13
    man... I've tried all of these things... still shaking. I can't however make my player into "is Kinematic" because then I can't apply forces to it (which is what my movement and flying scripts use. I have to use this type of movement script so my character (who is a flying squirrel) can climb trees. and now... he can fly, but when I turn the character shakes a lot. (only when I'm flying and turning)

    SmoothFollow camera on. Interpolate on player = none. When I changed from LateUpdate to Fixed update, the whole of everything is jilted, very choppy movement.
     
  19. zoxmax1

    zoxmax1

    Joined:
    Dec 18, 2010
    Posts:
    154
    I had same problems with riggid body attached on gameObject. I solved it in this way.I just break kinematic i one frame and again break afther 1 sec.It is js

    var a = 0;
    function Update () {
    if (a == 0) {
    GetComponent.<Rigidbody>().isKinematic = true;
    a = 1;
    fd ();
    }

    }

    function fd () {
    GetComponent.<Rigidbody>().isKinematic = false;
    yield WaitForSeconds (1);
    a = 0;

    }

    :)
     
  20. SkillBased

    SkillBased

    Joined:
    Aug 11, 2014
    Posts:
    141
    Thought I'd throw in my experience here for posterity.

    I too had these annoying issues with shaking/vibrating. Depending on the "variables" I changed it either caused the camera to shake or the object its following.

    By "variables" I actually mean things like:

    Camera Movement script using LateUpdate vs FixedUpdate vs Update (technically, it's not moving with physics so it should always be in LateUpdate...tried anyhow)

    Putting physics in Update (again, technically not what I'm supposed to do)

    etc, etc..

    All of these kluge type fixes not only didn't work and just transferred the shaking to another entity, but are bad practice in general.

    So, in the end what I did was set my object that the camera is following to "interpolate". I still got a bit of jitter in the editor but when I build the project the jittering/shaking/vibrating all goes away. That's it. Simple and you don't have to mess with how functions are meant to be used (update, fixedupdate, lateupdate).
     
    XCO and thedude9005 like this.
  21. thedude9005

    thedude9005

    Joined:
    Aug 22, 2017
    Posts:
    1


    You sir just saved my ass. That was simple and completely fixed the issue. Thanks so much, and I hope others found this at the bottom lmao.
     
    oryman7 and XCO like this.
  22. GameDeva

    GameDeva

    Joined:
    Dec 5, 2014
    Posts:
    2

    ------
    Thank you so much!
     
    oryman7 and XCO like this.
  23. xainulabdeen

    xainulabdeen

    Joined:
    Apr 10, 2018
    Posts:
    5
    i solved my issue in my game as i was working with ball and ground and both objects contained physics material i decreased the bounce value from ground material and increased in the ball material . !!!:)
     
  24. WayneJP

    WayneJP

    Joined:
    Jun 28, 2019
    Posts:
    44
    You just save my life!
     
  25. GameDevMark

    GameDevMark

    Joined:
    May 12, 2016
    Posts:
    2
    Legend!
     
  26. akashajith

    akashajith

    Joined:
    Jun 29, 2019
    Posts:
    7
    Thank You.
     
  27. TacoEric69

    TacoEric69

    Joined:
    Mar 11, 2023
    Posts:
    1
    Thanks kind Stranger. Ackerschlepper_endokrine_Pestizide_760.jpg
     
  28. oryman7

    oryman7

    Joined:
    Jul 10, 2022
    Posts:
    1
    I LOVE YOU MAN, THANKS!!!