Search Unity

Question Two objects with same RigidBody2D values are moving at different speeds despite equal forces applied

Discussion in 'Physics' started by K-bonky_, May 22, 2023.

  1. K-bonky_

    K-bonky_

    Joined:
    Jun 10, 2022
    Posts:
    2
    I am messing around with different ways of creating movement and two of my objects are moving at different speeds despite the forces being applied being equal. all of the values for these objects are the exact same except for the way the forces are applied.

    in both of the scripts the moveSpeed value equals 1
    the code for one of the objects is:
    Code (CSharp):
    1. private void FixedUpdate()
    2.     {
    3.         float xMovement = Input.GetAxis("Horizontal");
    4.         float yMovement = Input.GetAxis("Vertical");
    5.         Vector2 movement = new Vector2(xMovement, yMovement);
    6.         rb.AddForce(movement * moveSpeed, ForceMode2D.Impulse);
    7.     }
    8.  
    9.     private void LateUpdate()
    10.     {
    11.         if (rb.velocity.x > moveSpeed)
    12.         {
    13.             rb.velocity = new Vector2(rb.velocity.x - (rb.velocity.x - moveSpeed), rb.velocity.y);
    14.         }
    15.         else if (rb.velocity.x < -moveSpeed)
    16.         {
    17.             rb.velocity = new Vector2(rb.velocity.x - (rb.velocity.x - -moveSpeed), rb.velocity.y);
    18.         }
    19.         if (rb.velocity.y > moveSpeed)
    20.         {
    21.             rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y - (rb.velocity.y - moveSpeed));
    22.         }
    23.         else if (rb.velocity.y < -moveSpeed)
    24.         {
    25.             rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y - (rb.velocity.y - -moveSpeed));
    26.         }
    27.     }
    and the other is:
    Code (CSharp):
    1.    void Update()
    2.     {
    3.  
    4.         if (Input.GetKey(KeyCode.W))
    5.         {
    6.             rb.AddForce(Vector2.up * moveSpeed, ForceMode2D.Impulse);
    7.         }
    8.         if (Input.GetKey(KeyCode.A))
    9.         {
    10.             rb.AddForce(Vector2.left * moveSpeed, ForceMode2D.Impulse);
    11.         }
    12.         if (Input.GetKey(KeyCode.S))
    13.         {
    14.             rb.AddForce(Vector2.down * moveSpeed, ForceMode2D.Impulse);
    15.         }
    16.         if (Input.GetKey(KeyCode.D))
    17.         {
    18.             rb.AddForce(Vector2.right * moveSpeed, ForceMode2D.Impulse);
    19.         }
    20.     }
    21.  
    22.     private void LateUpdate()
    23.     {
    24.         if(rb.velocity.x > moveSpeed)
    25.         {
    26.             rb.velocity = new Vector2(rb.velocity.x - (rb.velocity.x - moveSpeed), rb.velocity.y);
    27.         } else if(rb.velocity.x < -moveSpeed)
    28.         {
    29.             rb.velocity = new Vector2(rb.velocity.x - (rb.velocity.x - -moveSpeed), rb.velocity.y);
    30.         }
    31.         if(rb.velocity.y > moveSpeed)
    32.         {
    33.             rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y - (rb.velocity.y -moveSpeed));
    34.         } else if(rb.velocity.y < -moveSpeed)
    35.         {
    36.             rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y - (rb.velocity.y - -moveSpeed));
    37.         }
    38.     }
    does anyone understand why it's doing this?
     
  2. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    601
    The impulse which you apply might be identical, but rate at which you apply them isn't.

    In the first case you do it every fixed update (by default 50 times a second).

    In the second case you do it in update which happens at the rate of whatever your FPS is which can be all over the place.
     
    K-bonky_ and MelvMay like this.
  3. K-bonky_

    K-bonky_

    Joined:
    Jun 10, 2022
    Posts:
    2
    Thank you so much, I can't believe I missed something so simple.