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

Bug AddForce cause inconsistent jumping

Discussion in '2D' started by imambabba, Aug 18, 2023.

  1. imambabba

    imambabba

    Joined:
    Feb 4, 2022
    Posts:
    5
    I have a little game project going on, im trying to copy Icy Tower. But there is a little problem with that. In icy tower when the player reaches max amount of velocity at x axis player jumps higher than normal amount and it rotates. I tried to copy that heres a code for that:

    Code (CSharp):
    1.  
    2. isGrounded2 = Physics2D.OverlapBox(groundCheck.position, new Vector2(1.08f, 1.58f), 0, groundLayer);
    3.  
    4.  
    5.         //This is a lambda expression it basically means if absolute value of players rigidbody veloicty x is bigger than whatVelocityToMultiply(its 12) jumpMultiplier variable is 1.7f same thing goes for other one altWhatVelocityToMultiply is 9
    6.  
    7.         float jumpMultiplier = (Mathf.Abs(rgbd2d.velocity.x) > whatVelocityToMultiply) ? 1.7f : (Mathf.Abs(rgbd2d.velocity.x) > altWhatVelocityToMultiply) ? 1.3f : 1f ;
    8.  
    9.  
    10.         if ((Input.GetKey(KeyCode.Space)) && (rgbd2d.velocity.y <= 0.01f) && (isGrounded2) && (relativeCheck.relativeVelocityCheck))
    11.         {
    12.             jumping = true;
    13.             rgbd2d.AddForce(forceDirection * jumpAmount * jumpMultiplier, ForceMode2D.Impulse);
    14.             Debug.Log(jumpMultiplier);
    15.         }
    16.  
    This void method called on FixedUpdate

    Here is a video explanation for this (I slow down the part for better understanding)



    I looked up about AddForce inconsistenty but i couldnt found anything useful
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    There's nothing inconsistent about AddForce as you imply. Your first thought should be what are you doing that is inconsistent. You need to firsr debug what impulse you're applying. If it's inconsistent, then there's your answer. If it's consistent then it'll be how often you are calling it.
     
  3. imambabba

    imambabba

    Joined:
    Feb 4, 2022
    Posts:
    5
    Even if i have all constant variables still has the same problem, sometimes it jumps very very little as if its bouncing
     
  4. imambabba

    imambabba

    Joined:
    Feb 4, 2022
    Posts:
    5
    But i found a important part about this bug on my game, it happens every time i tried to jump on the sides maybe it has to do something with PlatformEffector2D that i use for one way platforms
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    But again, you're implying that physics is somehow going wrong or being inconsistent rather than you indicating anything to do with how you're using it. I know it's often easier to assume something else is broken rather than your code but the code above doesn't show anything really so it's hard to help further.

    The only thing to see is that you're reading input during the FixedUpdate (you indicated you called the above code from there) which is something you should never do; input state needs to be read per-frame.