Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
This question was closed Jul 02, 2017 at 09:31 AM by hexagonius for the following reason:

The question is answered, right answer was accepted

avatar image
2
Question by Hiti3 · Sep 09, 2015 at 06:55 PM · velocityfixedupdatejitter2d-sidescroller

How to fix minor jitter/stutter when moving a 2D character using physics in FixedUpdate()

Hey there, please help me out I'm searching the web for over a week to find a solution about why is my movement "jitterish" on a random intervals from 3-7 seconds I get 1 minor jitter.

PROBLEM: Receiving jitter on random times, 1 minor stutter/jitter per 3-5 seconds. Some say its not visible but it sure is, because I'm making an android game where your prediction of how much distance is to the next obstacle is really important. I'm amazed of how are other one tap/jump games made with 0 jitter.

I am receiving jitter in Editor and on standalone android apk.

I'm handling inputs in Update and moving in FixedUpdate. Camera follows in the LateUpdate.

What I have allready tried:

Update and LateUpdate methods:

  • transform.translate + Time.deltaTime (bad idea, i know)

  • rigidbody2d.velocity + Time.deltaTime (still bad idea)

FixedUpdate method (Timestep: 0.01333 / 75 fps):

  • rigidbody2D.AddForce

  • rigidbody2D.velocity + Interpolation/Extrapolation (my current one)

Nonetheless which option I choose it still jitters.

Working example in Unity WebPlayer

Here is the code:

In the first class I have my camera follow the player. And in the second one there is player movement and other controlls.

Camera script:

 using UnityEngine;
 using System.Collections;
 
 public class CameraScript : MonoBehaviour {
 
     public Transform player;
     public Vector3 offset;
 
     private Vector3 targetPos;
     
     void LateUpdate () 
     {  
         // Smooth follow of the camera
         targetPos = new Vector3(player.position.x + offset.x, transform.position.y, transform.position.z);
         transform.position = Vector3.Lerp(transform.position, targetPos, 6.0f * Time.deltaTime);
     }
 }
 

Movement script:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
     
     public float moveSpeed = 0;
     public float jumpHeight = 0;
     public Rigidbody2D playerRigidbody;
     public Transform playerTransform;
 
     public Animator anim;
     
     private bool grounded = false;
     private bool jump = false;
     private Transform groundCheck;
 
     private Vector3 start;
     
     void Awake () 
     {
         // Find the groundCheck child transform
         groundCheck = transform.Find ("groundCheck");
     }
 
     void FixedUpdate()
     {
         // Move the player using physics
         playerRigidbody.velocity = new Vector2(moveSpeed, playerRigidbody.velocity.y);
         
         if (jump) {
             playerRigidbody.velocity = new Vector2(playerRigidbody.velocity.x, jumpHeight);
             jump = false;
         }
     }
 
     void Update ()
     {
     
         // Cast a line to the ground so we cant jump while in mid-air
         grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
 
         #if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBPLAYER
                 
             if(Input.GetKeyDown(KeyCode.UpArrow) && grounded) {
                 jump = true;
             }
                 
         #elif UNITY_IOS || UNITY_ANDROID || UNITY_WP8 || UNITY_IPHONE
                 
             if(Input.touchCount > 0){
                 Touch[] myTouches = Input.touches;
                 
                 // Handling multi-touch input
                 for(int i = 0; i < Input.touchCount; i++){
                     Touch myTouch = Input.GetTouch(i);
                     
                     if(grounded && myTouch.phase == TouchPhase.Began) {
                         jump = true;
                     }
                 }
             }
         #endif
     }
 }


Comment
Add comment · Show 3
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 Rostam24 · Sep 11, 2015 at 07:47 AM 0
Share

Any chance you have some debug.log() calls somewhere in your script? If so, try commenting them all out, and try again.

avatar image Hiti3 Rostam24 · Sep 11, 2015 at 07:49 AM 0
Share

They are off, I also tested that out. This is very strange...

avatar image JPBA1984 · Jul 02, 2017 at 07:38 AM 0
Share

1- Check for integers where floats should be, add the "f" even in the vars that are declared as floats, if you leave the mouse over the number a few secs it will tel if the compiler is interpreting it as a float or integer, the rounding of the integer could be an issue in smoothing.

2- move Time.deltaTime calculations to before the transforms, imagine the sequence, as soon as the LateUpdate begans assign it to something like float deltaTime = Time.deltaTime;

3- Get rid of the for iteration on the Update please, they tend to block the thread.

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by felixwcf85 · Mar 26, 2017 at 02:10 PM

Solved my problem on switching from FixedUpdate to LateUpdate. I'm using ProCamera2D and Jelly Sprite third part assets.

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
1

Answer by el-RERS · Sep 10, 2015 at 10:57 PM

have you tried having your camera script also in a FixedUpdate function? why does it need to be in LateUpdate?

Comment
Add comment · Show 6 · 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 GiyomuGames · Sep 11, 2015 at 02:47 AM 1
Share

As far as I know the camera script should always be in LateUpdate. It is specified clearly in the doc. http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.LateUpdate.html

avatar image el-RERS GiyomuGames · Sep 11, 2015 at 03:00 AM 0
Share

yeah but remember that LateUpdate updates every frame and FixedUpdate updates every few milliseconds so you will always get a slight delay between both. The documentation says that you can use LateUpdate only if your movement is in the Update function, but in your case, it is not, it's in FixedUpdate. TLDR: Try putting your camera in fixed Update ins$$anonymous$$d, it should fix the jittering.

avatar image Hiti3 el-RERS · Sep 11, 2015 at 07:43 AM 0
Share

I forgot to mention that I have allready tried putting it in the fixed update however it stutters even harder, thats because there isno interpolation on the camera, is there a wayto turn it on for the camera? Because if my player was without interpolation he stutters very hard, with the interpolation on its way better. So I Dont think its the camera making the problems, because I can see the litttle jitter also in the scene view where the camera doesnt follow the player.

Show more comments
avatar image Hiti3 · Sep 20, 2015 at 03:40 PM 0
Share

Please anyone!? Bump :( Is the problem inside the cameraobject? WHY does it stutter when I move it in fixed update?

The player ALSO stutters when I use movement in Fixed Update WITHOUT Interpolation...

avatar image hexagonius · Jul 02, 2017 at 09:30 AM 0
Share

No matter when anything moves in FixedUpdate, frames are still rendered on the render thread. And that's why the camera always moves in LateUpdate. That's the time when a frame is rendered and that's when the state of the game matters.

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

AddExplosionForce after FixedUpdate? 1 Answer

Camera Jitter / Stutter 1 Answer

Is my (LateUpdate) camera + rigidbody causing jitter? 2 Answers

Acceleration of Transform in FixedUpdate (SOLVED) 0 Answers

Get Interpolated rigidbody velocity 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