Search Unity

Velocity increases faster after large force is applied.

Discussion in '2D' started by Deci260, Sep 20, 2019.

  1. Deci260

    Deci260

    Joined:
    Sep 15, 2019
    Posts:
    1
    Hello, I'm working on a small project where a ball can either jump or roll. Basically when the user holds the left mouse button I call the
    Rigidbody2D.addForce()
    function with a really small value on the x to slowly increase the velocity until it gets to the max (10). Here's a small part of the
    Update()
    function where I decide how much force I'm going to add:

    Code (CSharp):
    1.  
    2. if(rb.position.x < point2.x)
    3. {
    4.       moveVelocity = new Vector2(0.1f, 0);
    5. }
    6.                    
    7. else
    8. {
    9.       moveVelocity = new Vector2(-0.1f, 0);
    10. }
    And then the
    FixedUpdate()
    function:

    Code (CSharp):
    1. if(!jumping)
    2.         {
    3.             rb.AddForce(moveVelocity, ForceMode2D.Impulse);
    4.         }
    By using the code above the ball gets to the mavelocity in around 5 seconds.
    As I said, the ball can also jump and it does that when the user drags the mouse while holding the left button. The force on the jump is calculated by the distance covered by the mouse, here's part of the code that does that:

    Code (CSharp):
    1. var forcey =  (mouseClickPosition.y - Input.mousePosition.y) / 25;
    2. var forcex =  (mouseClickPosition.x - Input.mousePosition.x) / 25;
    3.  
    4. jumpVelocity = new Vector2(forcex, forcey);
    Like before, the
    jumpVelocity
    vector will be used as the arg in a
    Rigidbody2D.addForce()
    call in the
    FixedUpdate()
    function.

    To slowly decrease the velocity after a jump or a roll I'm using the same code as when calculating the force for the rolls but with swapped values and with a stop when the velocity gets to 0.

    The code above works fine until the user actually jumps. After the landing both the decrease in velocity and the increase to 10 when rolling are instantaneous. I have no Idea why this is happening. I would really appreciate if you could point me in the right direction.
    Thanks.