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. Dismiss Notice

Player Stuttering when moving unity 2d

Discussion in '2D' started by DDAAVVIID, May 15, 2021.

  1. DDAAVVIID

    DDAAVVIID

    Joined:
    Dec 15, 2018
    Posts:
    17
    whenever the player uses its jetpack or when there's a lot of velocity, the player stutters. I tried using interpolate and other things like that but the only outcome was even more stutter. If anyone knows what the cause for the stuttering is, please tell me and if you want to, maybe even explain your answer :)

    here is the player code :

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.SceneManagement;
    3.  
    4. public class Player : MonoBehaviour
    5. {
    6.     //basic variables
    7.     [SerializeField]
    8.     private float speed = 5f;
    9.  
    10.     [SerializeField]
    11.     private float Jumpforce = 5f;
    12.  
    13.     [SerializeField]
    14.     private float JetPackForce = 5f;
    15.  
    16.     [SerializeField]
    17.     public bool canUseJetpack = true;
    18.  
    19.     [SerializeField]
    20.     private Rigidbody2D rb;
    21.  
    22.     [SerializeField]
    23.     private Animator animator;
    24.  
    25.     private bool isFacingRight = true;
    26.  
    27.     //runs when game starts
    28.     void Start()
    29.     {
    30.         rb = GetComponent<Rigidbody2D>();
    31.     }
    32.  
    33.     //runs every frame
    34.     void Update()
    35.     {
    36.         //applies force thus making player to move left or right
    37.         var move = Input.GetAxis("Horizontal");
    38.         transform.position = transform.position + new Vector3(move * speed * Time.deltaTime, 0, 0);
    39.  
    40.         //changes player animation state
    41.         animator.SetFloat("Speed", Mathf.Abs(move));
    42.  
    43.         //flip the player if facing right or left
    44.         if (move < 0 && isFacingRight)
    45.         {
    46.             flip();
    47.         }
    48.         else if (move > 0 && !isFacingRight)
    49.         {
    50.             flip();
    51.         }
    52.  
    53.         //checks if the space key is pressed and that the player velocity on the y axis is smaller than 0.001
    54.         if(Input.GetKeyDown("space") && Mathf.Abs(rb.velocity.y) < 0.001f)
    55.         {
    56.             //adds force from below the player thus making the player jump
    57.             rb.AddForce(new Vector2(0, Jumpforce), ForceMode2D.Impulse);
    58.         }
    59.  
    60.         //checks for the key 'Q' to be pressed and that there is enough fuel in the jetpack
    61.         if (Input.GetKey(KeyCode.Q) && canUseJetpack == true)
    62.         {
    63.             //ads force from below the player thus making the player fly using its jetpack
    64.             rb.AddForce(Vector2.up * JetPackForce);
    65.             //decreases the fuel in the jetpack
    66.             JetpackBar.instance.UseFuel(0.1f);
    67.             //makes the player run the using jetpack animation
    68.             animator.SetBool("UsingJetpack", true);
    69.         }
    70.         else
    71.         {
    72.             //if the player isn't using the jetpack, switch to the idle animation or the run animation depending if the player is moving or not
    73.             animator.SetBool("UsingJetpack", false);
    74.         }
    75.  
    76.         //checks if the player health is less than 0
    77.         if (HealthBar.instance.currentHealth <= 0)
    78.         {
    79.             //if so, restart the game
    80.             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    81.         }
    82.     }
    83.  
    84.     //checks if someting collided with the player
    85.     private void OnCollisionEnter2D(Collision2D collision)
    86.     {
    87.         //if the thing collided with the player has a tag of 'Enemy'
    88.         if (collision.gameObject.CompareTag("Enemy"))
    89.         {
    90.             //then, make the player take damage
    91.             HealthBar.instance.takeDamage(25);
    92.         }
    93.     }
    94.  
    95.     //flip the player
    96.     void flip()
    97.     {
    98.         isFacingRight = !isFacingRight;
    99.         transform.Rotate(0f, 180f, 0f);
    100.     }
    101. }
    and here is a video showing you what i mean by stuttering :

    2021 05 15 16 16 38 - YouTube
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529
    The whole point of adding a Rigidbody2D is so that it can control the transform pose. You're stomping over the Transform position with your own code. You should never modify the Transform when using 2D physics components. The odd thing is that you're then using forces to "jump" hoping that the Rigidbody2D can somehow override you stomping over the Transform with your position.

    All movements should go through the Rigidbody2D API.

    Think about it, how would a setting on the Rigidbody2D such as interpolation help when you are fully in control of the Transform position (you write to it each frame)? The answer is it won't help.
     
  3. DDAAVVIID

    DDAAVVIID

    Joined:
    Dec 15, 2018
    Posts:
    17
    can you please give me a hint on what i should replace my code with?
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529
    It needs a tutorial like this really.

    Look at the Rigidbody2D API for a start. Look at how you can move the Rigidbody2D and know it should be done in the FixedUpdate as physics runs by default then unless you set it to run per-frame. Add forces to move or set the X velocity for sideways movement being careful to not overrite the Y velocity component you use for vertical movement.
     
    DDAAVVIID likes this.
  5. DDAAVVIID

    DDAAVVIID

    Joined:
    Dec 15, 2018
    Posts:
    17
    thanks so much :D