Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Camera Jittering - Rigidbody Velocity and NavMeshAgent

Discussion in 'Cinemachine' started by HTFClem, Dec 28, 2018.

  1. HTFClem

    HTFClem

    Joined:
    Sep 5, 2018
    Posts:
    19
    Hi!

    As I am sure many topics are about, I'm going to talk about a stutter problem with a follow camera. The problem is not proper to Cinemachine, but it also happens with it, and I would like your insight on how I could approach the issue.

    Our game character is moved using Rigidbody.velocity for fine tuning while keeping good collision handling.
    However, we also have NavMeshAgents as part of the game who follows our character. Problem is, when we have a camera using our character as a pivot, if we update the camera in FixedUpdate, it "follows" the Rigidbody perfectly, but the NavMeshAgents look like they are jittering. If we use Update, the agents are fine, but the character appears to be jittering.

    I know it's most likely due to the transform of the Rigidbody controlled object being updated at a different rate than the rest, but at the moment we have no solution (note that Interpolation doesn't have any effect on that.. probably because I set the velocity?).

    Any idea what approach we could use to solve this and have a camera following the character without any jittering and without NavMeshAgent looking bad?

    Again, I understand this is not an issue caused by Cinemachine, but it is related to my use of it and I thought the best people to have an idea for that would be in that forum : )

    Thanks,

    Clem
     
  2. Multithreaded_Games

    Multithreaded_Games

    Joined:
    Jul 6, 2015
    Posts:
    122
    You could try disabling the automatic position updates given by the NavMeshAgent and move them from within FixedUpdate. If you set 'updatePosition' to false, a velocity (or desired velocity for obstacle avoidance) will still be generated by the NavMeshAgent, you'd just want to apply it as a velocity from within FixedUpdate.
     
  3. HTFClem

    HTFClem

    Joined:
    Sep 5, 2018
    Posts:
    19
    Thanks for the idea, I've tried that, problem is that avoidance is not as good while doing that. For instance, with the "normal" avoidance, if I walk around a bunch of NavMeshAgent with my character (which a NavMeshAgent without position update), if pushes them away gracefully, while when using desiredVelocity, they just stay where they are : (

    Also, I potentially have tons of NavMeshAgent, but only one main character, so ideally if the work around was on the character, that would have a much smaller performance impact than adding hundreds of FixedUpdate running.
     
  4. Multithreaded_Games

    Multithreaded_Games

    Joined:
    Jul 6, 2015
    Posts:
    122
    Yeah, that's definitely the simpler solution of the two. All right, so this is a problem we had to deal with ourselves and I swear I've re-worked our S*** about 50 times--but I think I finally hit on a doable solution.

    There are two main questions: does your character need to jump and does your character need to actually respond to collisions with forces? In my opinion, having the camera in fixed update is less than optimal and like you, we had a lot of problems using physics and interpolation--namely, it just never worked out correctly.

    Ultimately, we tore out all the physics and move characters now with translate from within Update and the camera is controlled from within LateUpdate. Maybe your project needs physics (ours is a role-playing game where the character can't actually jump) but if not, I can elaborate more on what we're doing if you'd think it'd be applicable! :)
     
  5. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    @HTFClem As you surmised, the judder problem stems from animating on different clocks. The only way to get rid of it is to animate everything on the same clock.

    It is definitely more performant to have the CM brain run in LateUpdate than in FixedUpdate, so if you can manage to do that it would be best.

    Normally, Interpolation on a RigidBody should allow physics-animated stuff to be compatible with Update-clock cameras. That's the point of it. @Multithreaded_Games if you've been unable to get that to work with CM, I'd love to hear some details about why. Most problems that people have had with it are a result of judder when damping is used, because of frame spikes. There is some experimental code that you can enable in CM to address this.
     
    Ryuuguu likes this.
  6. HTFClem

    HTFClem

    Joined:
    Sep 5, 2018
    Posts:
    19
    @Multithreaded_Games Thanks, we actually used to not have any kind of jump, and we were using full NavMeshAgent controls instead, which didn't have any issue with Cinemachine (yay!), however we now want to try to add platforming to the game that is why we went for a Rigidbody. I could potentially do jumping without a Rigidbody but I'd prefer not to avoid potential issues with collision and all that. But definitely agree with you there, on our other game we use NavMeshAgent velocity for player movement instead of Rigidbody and it's been so much easier! If I don't need physics, I always avoid it.

    @Gregoryl Thanks for your reply. I thought the same about interpolation that is why I was surprised it didn't work at all. I've made many tests with no real improvement. At the moment my fix has been to interpolate manually the position of the renderer object between the Rigidbody object FixedUpdate positions, and the camera follows in LateUpdate, but it's a hack that I would rather not have to do. I have actually made this thread in the Physics forum about it too yesterday: https://forum.unity.com/threads/201...-effect-when-using-rigidbody-velocity.604573/
    I would be interested in knowing what part of what I am doing is breaking interpolation. If I didn't use velocity, I would understand, but that's all I am doing! : (

    Thank you both for your input, I think I will see if I get an answer from the Rigidbody Interpolation thread as it is now clear that it's the main issue there.