• 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
Question by desertedgamesstudio · Aug 17, 2020 at 07:55 AM · rigidbodyupdatefixedupdateinterpolationslowmotion

How do I fix jitter?

So I'm currently making a rigidbody fps controller and I'm getting t$$anonymous$$s jitter that comes in spurts. It's not horrible but becomes very bad when switc$$anonymous$$ng to slow motion. The jitter only happens when I'm walking around and looking at an object. The player is already interpolated as well.

Here's my player script:

 public class PlayerController : MonoBehaviour
 {
     //Components
     private Animator anim;
     private Rigidbody rb;
 
     //Movement
     Vector3 movement;
 
     //Speed and Sprinting
     private bool sprinting;
     private float speed;
     public float sprintSpeed = 750f;
     public float walkSpeed = 500f;
     public float stamina, maxStamina;
     public bool usingStamina;
 
     //Jump and Grouncheck
     public float jumpForce;
     public bool isGrounded;
     public Transform groundCheck;
     public float groundDistance = 0.4f;
     public LayerMask whatIsGround;
     
     public Vector3 airControl;
     public float airControlForce;
 
   
 
 
 
     // Start is called before the first frame update
     void Start()
     {
         rb = GetComponent<Rigidbody>();
         anim = GetComponent<Animator>();
        
     }
 
     // Update is called once per frame
     void Update()
     {
         movement.x = Input.GetAxisRaw("Horizontal") ;
         movement.z = Input.GetAxisRaw("Vertical");
 
         movement.Normalize();
 
         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, whatIsGround);
            
         if (isGrounded)
         {
             Jump();
         }
 
         Sprint();
            
     }
 
     private void FixedUpdate()
     {
 
 
         if (isGrounded)
         {
 
             rb.velocity = rb.rotation * new Vector3(movement.x * speed * Time.deltaTime, rb.velocity.y, movement.z * speed * Time.deltaTime);
       
         }
                     
        if (!isGrounded)
         {
 
             rb.AddRelativeForce(movement.x * airControlForce, 0f, movement.z * airControlForce);
     
         }
   
     }
 
     private void Jump()
     {
 
         if (Input.GetKeyDown(KeyCode.Space))
         {
             rb.AddForce(jumpForce * transform.up);
         }
       
     }
 
     private void Sprint()
     {
         speed = walkSpeed;
 
         if (isGrounded)
         {
             if(Input.GetKeyDown(KeyCode.LeftS$$anonymous$$ft))
             {
 
                 sprinting = true;
 
             }
                         
         }
 
         if (Input.GetKeyUp(KeyCode.LeftS$$anonymous$$ft))
         {
 
             sprinting = false;
 
         }
 
        if(usingStamina)
         {
             if (sprinting && stamina >= 0f)
             {
                 speed = sprintSpeed;
                 stamina -= Time.deltaTime;
             }
             else if (!sprinting && stamina < maxStamina)
             {
                 stamina += Time.deltaTime;
             }
         }
        else if(sprinting)
         {
             speed = sprintSpeed;
         }
     
     }
    
     private void OnDrawGizmos()
     {
         Gizmos.DrawWireSphere(groundCheck.position, groundDistance);
     }
 
 }

And here's my camera script:

 public class CameraController : MonoBehaviour
 {
     public Vector2 mouseLook;
     [Range(0.0f, 100f)]
     public float sensitivity;
 
     private GameObject character;
     private Rigidbody rb;
     void Start()
     {
         Cursor.lockState = CursorLockMode.Locked;
         Cursor.visible = false;
         character = transform.parent.gameObject;
         rb = character.GetComponent<Rigidbody>();
     }
 
     private void LateUpdate()
     {
         mouseLook.x += Input.GetAxis("Mouse X") * sensitivity;
         mouseLook.y += Input.GetAxis("Mouse Y") * sensitivity;
         mouseLook.y = Mathf.Clamp(mouseLook.y, -90f, 90f);
 
 
         transform.localRotation = Quaternion.AngleAxis(-mouseLook.y, Vector3.right);
         rb.MoveRotation(Quaternion.AngleAxis(mouseLook.x, rb.transform.up));
     }
 
 
 }

I'm going to assume it has somet$$anonymous$$ng to do with updates vs fixed updates but I can't figure out what I did wrong.

Comment

People who like this

0 Show 1
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 _dns_ · Aug 17, 2020 at 02:42 PM 0
Share

Hi, looking at your code: some physics related code is called in Update, not in FixedUpdate and this will lead to bugs at high framerates. Example: on a situation where Update is called 2 times between 2 FixedUpdates (game at 120Hz, physics at 60Hz, or game & physics at 60Hz but timeScale at 0.5): the "jump" code can be called twice (during 2 consecutive Update) before the next FixedUpdate, the player will then jump higher (2 calls to AddForce before physics are resolved). The jitter might not be linked to that though, but those problems should be fixed first :-)

EDIT: my bad: GetKeyDown should avoid the double jump (except if it's possible to press the key faster than 2 physics update). Though, even if it works, it's not very "clean"

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by FlaSh-G · Aug 17, 2020 at 09:49 AM

Edit

ait a second, I overlooked that t$$anonymous$$s is a first person camera. LateUpdate (as well as Update, by the way) should work fine. However, you are mixing camera controls with controls for the rigidbody.

Rigidbody interpolation means that between two FixedUpdates, the object is interpolated between the second latest FixedUpdate result and the latest. So if you had, say, ~3 frames between two FixedUpdates (w$$anonymous$$ch is what you have at 144 fps), the object moves in 3 steps from the previous position to the current one.

Now, if you were to interfere in that interpolation by doing stuff with the rigidbody, I could imagine that t$$anonymous$$ngs could get hairy. Although you're doing movement in FixedUpdate and only rotation in Update, I could, for example, imagine that rotation interpolation could get into conflict with rotation applied in Update.

Original answer

ere, have t$$anonymous$$s article: http://blog.13pixels.de/2019/what-exactly-is-fixedupdate/

In it, there's a script linked that you can use: https://gist.github.com/FlaShG/2ee9ca42492363f25b1c416ac85b0732

In short: Move the camera in FixedUpdate as well and interpolate it with that script. If you still get jitter, interpolate your rigidbody with that script as well (and turn interpolation off on the rb itself).

Comment
desertedgamesstudio

People who like this

1 Show 0 · 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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

187 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

Related Questions

Splitting one event's code between Update and fixedUpdate 2 Answers

Rigidbody.AddForceAtPosition & functions 1 Answer

Slow motion all but one - yet another round, hopefully the last 6 Answers

Updating at frameRate some fixedUpdated gameObject 1 Answer

Game not running properly on slow machines (low fps) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges