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
}
}