Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Player is jittering with high FPS

Discussion in '2D' started by denniscm190, Jan 17, 2023.

  1. denniscm190

    denniscm190

    Joined:
    Aug 13, 2021
    Posts:
    4
    Hello guys,

    I'm just starting to learn game development with Unity and I'm trying to understand the following issue that is happening:

    I have a basic 2D scene with a character and some tiles (all from the asset store) and a Cinemachine camera that is set to follow the character. The thing is, when I play the game in the editor and the FPS are high (600-700 says the "stats" window) the character jitters when running and jumping. However, after some minutes normally the FPS drops to 60-70 and the jitter stops. If I build and run the game, the jitter does not happen.

    Here are two videos for reference:
    - Smooth movement

    - Jittering


    I would like to know:
    - What causes the drop in FPS? It seems random because it does not always happen after playing in the editor.
    - Why is jittering playing in the editor but not in the game build.

    Thank you!
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,267
    What version of Unity are you using?
    Does the jitter happen if you close the inspector?
     
  3. gooby429

    gooby429

    Joined:
    Aug 13, 2019
    Posts:
    116
    can you show your movement script? if you havent resolved it already
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    One thing to note here is that if this is Rigidbody2D movement then the Rigidbody2D doesn't have interpolation activated therefore the Transform will only ever update when the simulation runs which, by default, is 50Hz.
     
  5. denniscm190

    denniscm190

    Joined:
    Aug 13, 2021
    Posts:
    4
    I'm using 2021.3.16 LTS. Right now the jitter is not happening. However, It would be great to know what was causing it. Maybe is my computer? It's a 2019 MacBook Pro i9 16GB.
     
  6. denniscm190

    denniscm190

    Joined:
    Aug 13, 2021
    Posts:
    4
    Sure, here is the player controller.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. public class PlayerController : MonoBehaviour
    7. {
    8.     [SerializeField] private float moveSpeed = 8f;
    9.     [SerializeField] private float jumpForce = 16f;
    10.     [SerializeField] private float groundCheckRadius = 0.2f;
    11.     [SerializeField] private float fallingThreshold = -0.1f;
    12.  
    13.     [Space(20)]
    14.  
    15.     [SerializeField] private Rigidbody2D rigidBody;
    16.     [SerializeField] private Transform groundCheck;
    17.     [SerializeField] private LayerMask groundLayer;
    18.     [SerializeField] private Animator animator;
    19.     [SerializeField] private PlayerAttackController attackController;
    20.  
    21.     private float _moveDirection;
    22.  
    23.     private bool _jump;
    24.     private bool _isGrounded;
    25.  
    26.     private bool _attack;
    27.     private bool _isAttacking;
    28.  
    29.     private bool _isFacingRight = true;
    30.  
    31.     // User inputs
    32.     public void Move(InputAction.CallbackContext context)
    33.     {
    34.         _moveDirection = context.ReadValue<Vector2>().x;
    35.     }
    36.  
    37.     public void Jump(InputAction.CallbackContext context)
    38.     {
    39.         if (context.performed && _isGrounded)
    40.         {
    41.             _jump = true;
    42.         }
    43.     }
    44.  
    45.     public void Attack(InputAction.CallbackContext context)
    46.     {
    47.         if (context.performed && _isGrounded)
    48.         {
    49.             _attack = true;
    50.         }
    51.     }
    52.  
    53.     void Update()
    54.     {
    55.         if (!_isFacingRight && _moveDirection > 0f)
    56.         {
    57.             Flip();
    58.         }
    59.         else if (_isFacingRight && _moveDirection < 0f)
    60.         {
    61.             Flip();
    62.         }
    63.     }
    64.  
    65.     void FixedUpdate()
    66.     {
    67.         _isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayer);
    68.         _isAttacking = animator.GetBool("PlayerMovementLocked");
    69.  
    70.         // Move
    71.         if (!_isAttacking)
    72.         {
    73.             rigidBody.velocity = new Vector2(_moveDirection * moveSpeed, rigidBody.velocity.y);
    74.             animator.SetFloat("MoveSpeed", Mathf.Abs(_moveDirection));
    75.         }
    76.  
    77.         // Jump
    78.         if (_jump && !_isAttacking)
    79.         {
    80.             rigidBody.AddForce(new Vector2(0f, jumpForce), ForceMode2D.Impulse);
    81.             animator.SetBool("IsJumping", true);
    82.             _jump = false;
    83.         }
    84.  
    85.         if (rigidBody.velocity.y < fallingThreshold)
    86.         {
    87.             animator.SetBool("IsJumping", false);
    88.             animator.SetBool("IsFalling", true);
    89.         }
    90.  
    91.         if (_isGrounded)
    92.         {
    93.             animator.SetBool("IsFalling", false);
    94.         }
    95.  
    96.         // Attack
    97.         if (_attack)
    98.         {
    99.             rigidBody.velocity = new Vector2(0f, 0f);
    100.             animator.SetTrigger("Attack");
    101.             attackController.Attack();
    102.             _attack = false;
    103.         }
    104.     }
    105.  
    106.     private void Flip()
    107.     {
    108.         _isFacingRight = !_isFacingRight;
    109.         Vector3 localScale = transform.localScale;
    110.         localScale.x *= -1f;
    111.         transform.localScale = localScale;
    112.     }
    113. }
    114.  
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    See my post above.
     
  8. denniscm190

    denniscm190

    Joined:
    Aug 13, 2021
    Posts:
    4
    Thanks for your response.

    I changed Interpolate from None to Interpolate and I think this resolved the issue. Thank you.
     
    MelvMay likes this.
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    Yes, it's important to understand that physics isn't running per-frame unless you ask for it to do so in the 2D physics settings.

    Good luck!