Search Unity

Camera jittering on moving platform

Discussion in 'Cinemachine' started by Jairusx, Jan 25, 2021.

  1. Jairusx

    Jairusx

    Joined:
    Jun 25, 2020
    Posts:
    62
    Hi. I have a weird camera jittering while my player is on the moving platform
    I use two cameras. One free-look camera and one virtual camera for my "aiming" purposes
    When I step on the moving platform, on activating free-look default camera, my environment is jittering.
    If I switch to my aim camera, the jitters transfer on my third person character.
    Jitter amount is lowering when I look ahead, and raises when my character turn left or right.
    I am using fixedUpdate to move my platform, since my character is rigidbody based.

    Here is a simple video that visualize my problem: https://imgur.com/IQWv4Fd

    Am I missing something? Thanks in advance! :)
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,720
    Looks like the vcam and the character are not animating on the same clock.
    • What is the Update Method setting in CM Brain?
    • When the project is running, look at the inspector of the active vcam. Next to the "Solo" button, it will show how the vcam is being updated (FixedUpdate or LateUpdate). What does it read for your 2 vcams, when the jitter is happening?
     
  3. Jairusx

    Jairusx

    Joined:
    Jun 25, 2020
    Posts:
    62
    Hey Gregoryl. My update method from main camera is set to "Smart update"
    On game running It says "late update"
    I tried switching it to "Fixed update" from the main camera. and that fixes the problem with platforms.
    However this way, my character start jitters when he is not on the platform :D
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,720
    What happens if you switch back to smart update and enable Interpolation on your platform's rigid body?
     
  5. Jairusx

    Jairusx

    Joined:
    Jun 25, 2020
    Posts:
    62
    I don't have any rigidbody on the platform itself. I am using a script to move the platform using transform.position mainly.
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,720
    In that case, it's very likely that you aren't moving the platform smoothly, or are not moving it exclusively in Update() or FixedUpdate().

    If your target character is animating on FixedUpdate() - perhaps because it is a rigidbody - but your platform is moving on Update(), that is a problem because the camera won't know how to follow it. What happens if you move the platform in FixedUpdate() only?
     
  7. Jairusx

    Jairusx

    Joined:
    Jun 25, 2020
    Posts:
    62
  8. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,720
    Try changing
    float distCovered = (Time.time - startTime) * speed;
    to
    float distCovered = Time.deltaTime * speed;
     
  9. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,720
    Sorry that suggestion was a little too naive. What I'm trying to get at is that this code
    Code (CSharp):
    1. float distCovered = (Time.time - startTime) * speed;
    2. float fractionOfJourney = distCovered / journeyLength;
    3. transform.position = Vector3.Lerp(departTarget.position, destinationTarget.position, fractionOfJourney);
    is questionable because of the use of time. Instead, can you try something like
    Code (CSharp):
    1. var velocity = (destinationTarget.position - departTarget.position).normalized * speed;
    2. transform.position += velocity * Time.deltaTime;
     
  10. Jairusx

    Jairusx

    Joined:
    Jun 25, 2020
    Posts:
    62
    This way (both ways actually) breaks my functionality of the platforms :D .. Maybe I should check my rigidbody. It's weird that changing the main camera update method to "fixed update" from "smart update" fixes my problem with the platforms, but open a problem with my character controller...
     
  11. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,720
  12. Jairusx

    Jairusx

    Joined:
    Jun 25, 2020
    Posts:
    62
    I tried testing your code, just to see is there any difference in the behavior of the player after replacing Time.time with time.deltaTime.
    I read the above topic and it is really informative.

    The player still jitters on the platform. Is it possible the issue to be in my player controller script?
     
  13. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,720
    Yes it is possible.
     
    Jairusx likes this.
  14. Jairusx

    Jairusx

    Joined:
    Jun 25, 2020
    Posts:
    62
    Ok. I don't have any idea why this, but it gets actually better after I turn character controller rigidbody Interpolate from "Interpolate" to "None"
    I hope it won't affect on my physics

    upload_2021-1-25_23-42-43.png
     
  15. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,720
  16. Jairusx

    Jairusx

    Joined:
    Jun 25, 2020
    Posts:
    62
    The thing is that if I turn on interpolate of my character rigidbody only "left" and "right" movement is jittery.. The forward and backwards movement is still smooth.

    Video: https://imgur.com/a/5Q80XJt

    After making platforms on update instead of fixedUpdate and turn on my interpolate I get jittery movement from the player not from the camera while the platform is moving.
     
  17. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,720
    I suspect that it's equally jittery in all directions, only you don't notice it front-to-back because the effect in perspective will seem very small when compared with side-to-side.

    How does the player move when it's on the platform? What makes the player not just stay still while the platform moves out from under him?
     
  18. Jairusx

    Jairusx

    Joined:
    Jun 25, 2020
    Posts:
    62
    I am making a similar to platformer game, so I would have platforms that move constantly and my player needs to be able to jump precisely from one to another.
     
  19. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,720
    ok, but my question is: what is the mechanism that makes the player stick to the platform when the platform is moving?
     
  20. Jairusx

    Jairusx

    Joined:
    Jun 25, 2020
    Posts:
    62
    Oh sorry. I am using very simple way to parent the player to the platform onCollisionEnter



    Code (CSharp):
    1.  
    2.  void OnTriggerExit(Collider other)
    3.     {
    4.      
    5.         if (other.gameObject.tag == "Player")
    6.         {
    7.             other.transform.parent = null;
    8.         }
    9.     }
    10.     void OnTriggerEnter(Collider other)
    11.     {
    12.         if (other.gameObject.tag == "Player")
    13.         {
    14.             other.transform.parent = transform;
    15.         }
    16.     }
    17.  
    18.  
    I have a second box collider with trigger function on each platform
     
  21. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,720
    So if I understand correctly, here is the situation in your game:
    - You gave a character with a RigidBody, whose transform is moved by physics in FixedUpdate
    - There is no interpolation on the character
    Is this correct?
    If so, can you try turning on Interpolation on the character?
    What I am trying to do is to get all transforms to move in Update (not FixedUpdate). That way, cameras can also update there and there should be no jitter.
     
  22. Jairusx

    Jairusx

    Joined:
    Jun 25, 2020
    Posts:
    62
    The situation is that I am moving my character in fixedUpdate and my platforms in fixedUpdate.
    When my main camera's update method is set to "smart update" , my character camera jitters on the platform.
    When my main camera's update method is set to "fixed update" my character itself starts to jitter but the platform jitterings disappear

    That is when my character has interpolate set to "interpolate"

    The solution I found to stop the jitters and stay again in "smart update" is to turn off my character interpolation and set it to "none"
    That way, at least for now, my character does not jitter on ground or platforms

    I am not sure if this is the right way regarding my physics and will it affect my gameplay, so I don't know if the problem is solved or not :D

    I tried making platforms move on update instead on fixedUpdate, but my character jitters when collides with that
     
  23. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,720
    ok, so you have implemented the alternative solution of animating everything in FixedUpdate(). That is good. The main thing is that there must be consistency: all on Update() or all on FixedUpdate().
     
  24. Jairusx

    Jairusx

    Joined:
    Jun 25, 2020
    Posts:
    62
    Do u think my lack of interpolation on third person controller rigidbody will cause any issues? Like fall from the map, stucking or something? For now I cannot see any difference in the negative aspect.

    Alternatively I found a more advanced way to get rid of jitter -


    But honestly, if it ain't broke, don't fix it :D
     
  25. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,720
    That's a very good video; it describes the issue well. Thanks, I had not seen it before.

    You should be fine doing everything in FixedUpdate. The only potential issue is very slightly uneven movement if you have too much aliasing between Update and FixedUpdate. It will only be a visual artifact, the game logic will not be affected. The extent of it will depend on your physics timestep.
     
    Jairusx likes this.
  26. Jairusx

    Jairusx

    Joined:
    Jun 25, 2020
    Posts:
    62
    U are welcome! It is very weird that the interpolation must "smooth" the jitter and in my case does the opposite.
    I think my problem is fixed for now. If I have any weird physics because of no interpolation on my character, I will try with the video above, and if I have any other issues I will open a new thread.
    As always u were so kind helping me with the problem. Thank you very much!
     
  27. Adesnada

    Adesnada

    Joined:
    Oct 14, 2020
    Posts:
    12
    Hi. I actually happen to have kind of the same problem. I don't really understand how you solved your problem because I feel I'm having the same one as you.

    First of all, after way too much googling for a week I found that this should always be true:

    Update() check for input
    FixedUpdate() move gameObjects that have riggidbody/apply forces
    LateUpdate() move Camera

    So that is what I did. I move my character in FixedUpdate() and my Camera in LateUpdate(). That causes jitter, of course, so I interpolate the character's rb and it does the trick. Everything is smooth as hell.
    Now, I try to do the same with the moving platform. I move it in FixedUpdate() and I set its rb to interpolate... but idk why, it jitters anyway, no matter what... when the player gets on it, it gets patented by the platform via Setparent but the jitter is horrible.
    So, the cause of that problem is obvious. The platform is moving in FixedUpdate() and the camera in LateUpdate() so their timesteps don't match and the only way to fix the problem would be to interpolate the platform's rb, but it won't work.

    What I have tried to solve this issue:

    I tried to change the platform's movement method to Update() so its timesteps would match with the camera's LateUpdate() one... and it works perfectly!!!
    Now the problem comes when the player tries to get on it. If you stand still everything is smooth, but when you try to move, the player's speed is so slow that you can barely move. It is like the player inherits the platform's Update() function and overrides the player's Fixedupdate() and because the player's speed depends by fixedDeltatime, it won't work in an Update() function.

    I have also tried to set the camera to FixedUpdate() and turn off all interpolation... everything jitters

    I have tried many other things but I think the problem resides in their timesteps.

    The solution I found and I dont like:

    Instead of moving the platform via script, I tried to animate the platform. It jitters like when it was moving with FixedUpdate(). However, in the animator component, there's a setting called update mode. I set it to animate physics to get rid of the jitter as the interpolation would do.... and all over the sudden everything works smoothly, even when the player gets on the platform and moves or stands still..

    This is not the optimal solution because you would have to create an animation for every movement behavior, like going up and down instead of left and right and so on..

    I dont know if this is a bug or there's something I'm missing...
     
    Last edited: Mar 2, 2021
  28. Jairusx

    Jairusx

    Joined:
    Jun 25, 2020
    Posts:
    62
    Hey. Try turning off the character rigidbody interpolate. I set it to None and it works for me... I think it may also depend on the controller. Also, do u try with the video I posted?
     
    Adesnada likes this.
  29. Adesnada

    Adesnada

    Joined:
    Oct 14, 2020
    Posts:
    12
    I tried but still jitters because the camera moves in LateUpdate and the character in FixedUpdate.
    The camera needs to be in LateUpdate so all callculations have finished before render the frame.
     
  30. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,720
    Move everything in FixedUpdate. Character, Camera, and Platform. Make sure interpolation is off on everything. It should be perfectly smooth.

    Interpolation "converts" the movement from FixedUpdate to LateUpdate, by interpolating according to elapsed time, so it effectively allows you to animate the camera in LateUpdate (which is more performant) HOWEVER interpolation only works for RBs animated by physics forces. If you manually animate the transform then interpolation won't happen. That's probably why it wasn't working for your platform.
     
    Adesnada likes this.
  31. Adesnada

    Adesnada

    Joined:
    Oct 14, 2020
    Posts:
    12
    I actually tried to move everything to fixed update and yes, everything was smooth... until I realised that the camera was being capped by the FixedUpdate timestep(0.02) so even tho my game was running at 120 fps, it looked like 40~50, so that didnt work either. I, of course, could change that to 0.01 and I'd get 60fps but I think that'd be a waste of performance.

    At first, when my platforms didn't work properly, I was animating them with physic forces (moveforward) and interpolation wouldn't work... it was when I was moving them by animations when everything worked (checking animate physics in the animator component)