- Home /
The question is answered, right answer was accepted
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
}
}
Any chance you have some debug.log() calls somewhere in your script? If so, try commenting them all out, and try again.
They are off, I also tested that out. This is very strange...
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.
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.
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?
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
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.
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.
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...
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
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