• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by U2-84 · Oct 17, 2019 at 02:24 PM · rigidbodyjittersmooth follow

Camera follow jitter

Hello guys, I know this is a common issue, I tried many suggested solutions but none worked for me.

I have a rigidbody (spaceship) moving in a 3D space; since I'm using forces, controls are in FixedUpdate and interpolation is active. The rigidbody is an empty GameObject, and the spaceship elements (engines, body, etc) are simple children of the GameObject. The controls script is oviously attached to the rigidbody (GameObject).

If I use a static camera (child of the spaceship), everything is smooth. If I use a "smooth follow camera", everything looks jittery (even though the "game" runs at stable 60fps with V-Sync on).

This is the camera script I'm using:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CameraFollow : MonoBehaviour
 {
 
     public Transform target;
     public float distance = 27.0f;
     public float height = 2.0f;
     public float damping = 0.5f;
     public float rotationDamping = 0.5f;
     
     void FixedUpdate()
     {
         Vector3 desiredPosition = target.TransformPoint(0f, height, -distance);
         transform.position = Vector3.Lerp(transform.position, desiredPosition, damping);
         Quaternion desiredRotation = Quaternion.LookRotation(target.position - transform.position, target.up);
         transform.rotation = Quaternion.Slerp(transform.rotation, desiredRotation, rotationDamping);
 
         transform.LookAt(target, target.up);
     }
 }
 

Now I'm wondering: the target of this script is a transform (and it's actually pointing to an element of the ship called "CameraTarget", an empty object, child of the main rigidbody)... Maybe the target should be a rigidbody itself? Or being a child of the rigidbody, basically a part of it, there's no difference?

Thank you in advance.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Ermiq · Nov 20, 2019 at 03:10 PM

After a long time I've spent trying to get smooth camera with no jittering while using rigidbody driven character I've ended up switching to CharacterController.

Yes, there are Rigidbody.Interpolation, different update loops for different things, and so on, but sadly it's never good enough. In some cases it's possible to get satisfying results, but it's never ideal unless you reinvent the wheel from scratch, like implementing custom update loops, custom interpolation methods and other stuff.

However, there's one thing I want to suggest. Maybe it will help you. Your Lerp() usage is wrong. It's very very common mistake though, and it caused by many not good enough lerp tutorials. The thing is, you use the damp parameter in Lerp(source, target, damp) functions in wrong way. It leads to a situations where the more the delay between frames/updates, the faster your camera have to move to reach the target position between updates, and it turns out that smoothing the camera this way makes the camera even more jittery.
The solution is to pre-damp/pre-smooth the dampparameter itself. Read this thread: https://forum.unity.com/threads/camera-stutters-in-a-simple-rotation.389899/ and what the user roridriscoll have wrote there. His lerp method works great for smooth camera following, and that's how all Lerp() methods should be done to be real smoothing methods.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image U2-84 · Nov 27, 2019 at 04:47 PM 0
Share

Hello, first of all, thanks for your answer @Ermiq . The camera code I posted is a quite old code found years ago on the Unity wiki page (if I recall correctly). I have read the thread you linked and I need to do some tests and research: it looks quite tricky.


Anyway, for a simpler/temporary solution, I managed to eliminate the jitter by placing both the rigidbody and camera scripts in FixedUpdate. I have read everywhere that camera should always go in LateUpdate, but LateUpdate and FixedUpdate occour at different times (once per frame vs once per physics step) so we get the jitter. Placing the camera in FixedUpdate (where it should go, since it's following an object moving in FixedUpdate), the jitter goes away but there's a constant micro-stuttering on the environment (noticeable when rotating or passing close to other objects), like it's going at a lower framerate. The cause is the visual (camera) moving in FixedUpdate, hence updating its position every 0.02 seconds (or 50 times per second). Apart from using Lerp in a different way as you suggested, I think it is an unavoidable issue regarding rigidbodies moving in FixedUpdate: they always tend to stutter (sometimes it's very subtle but a trained eye can spot it): so I decided to lower the fixed timestep to 0.01667 and now everything looks perfectly smooth, because physics (and everything running in FixedUpdate) now update 60 times per second, along with everything else in Update with V-Sync on.

avatar image
0

Answer by U2-84 · Nov 20, 2019 at 10:50 AM

A little update. The overall look is now smooth. Controls are still in FixedUpdate, while the camera is now handled in LateUpdate. But... there's a new problem now: if I look closely at my ship, I see it's "shaking" (slightly). The shake effect is more noticeable if I use a very close camera. I tried everything: both controls and camera in update, controls in update and camera in lateupdate, both controls and camera in fixedupdate, etc... They all give me worse results (more shaking and/or environment jitter/micro stuttering); the smoothest combination is controls in fixedupdate (since I'm moving a rigidbody) and camera in lateupdate. But the shaking ship is driving me crazy. Is there a way to have a camera smooth following physics objects without collateral issues? Edit: just before someone points it out, the shake effect isn't related to floating point precision. I'm using a floating origin system so the player ship is always close (<1000m) to the origin.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by WakingDragon · Jan 11 at 09:10 AM

A common reason for jitter is a script moving a gameobject to an accurate position, and never quite settling on it. So one technique to try is to expose a variable for "accuracy" and only attempt to move the gameobject if it is further from its target than the accuracy variable. In pseudo code...

 if(distanceToTargetOrAngleFromTargetAngle > accuracy)
 {
 //do your position, rotation, etc 
 }
 //else do nothing

There are other causes of jitter of course!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

225 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I avoid jitter of house? 0 Answers

Move a camera with Rigidbody velocity without jitter 1 Answer

Weird jitter on Android using Character Controller 0 Answers

Jittery world around stationary character 1 Answer

Character Controller movement jittery while standing on a rigidbody 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges