Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

2019.3.0f6 (and 2020.1) - ForceMode2D.Impulse randomly doesn't fire

Discussion in '2020.1 Beta' started by Deleted User, Dec 28, 2019.

  1. Deleted User

    Deleted User

    Guest

    @LeonhardPOn testing my game with 2019.3 or 2020.1, I noticed the following problem that I cannot explain.

    Description:

    The tests were made using two different players that both use the same Animator Controller but have their own animations with identical results:
    1. in the hanging position on the Capture 1, pressing the "Jump" button (the space bar) should make the player jump and pressing the right arrow makes her land on the platform. This works perfectly in 2019.2 whatever the version.
    2. now pressing the "Jump" button (the space bar) makes the player fall on the floor below altough the animation fires correctly. Capture 2 was made just after pressing the Jump button and you can see that the player is falling on the floor below instead of jumping and that the "Jump" animation is running.
    3. to worsen the whole thing, this happens only at the places where the player is on Captures 1 and 2 and at the positions in the other captures that I put in the spoiler section at the bottom of the thread.
    Why the code (below, under the captures) is working mostly and not in these specific positions is beyond my understanding, especially since this problem doesn't happen in previous versions of Unity. I don't think filing a bug report will be of any use in this case.

    Capture 1:

    Sans-titre-1.jpg

    Capture 2:


    Sans-titre-2.jpg

    Code involved:

    Code (CSharp):
    1.         jumpPressed = jumpPressed || Input.GetButtonDown("Jump");
    Code (CSharp):
    1.         if(isHanging)
    2.         {
    3.             if(playersInput.jumpPressed)          //playersInput is another script where
    4.                                                   //all the player inputs are stored
    5.             {
    6.                 isHanging = false;
    7.                 rigidBody.bodyType = RigidbodyType2D.Dynamic;
    8.                 rigidBody.AddForce(new Vector2(0f, hangingJumpForce), ForceMode2D.Impulse);
    9.                 return;
    10.             }
    11.         }
    Sans-titre-3.jpg

    Sans-titre-4.jpg

    Sans-titre-5.jpg
     
    Last edited by a moderator: Feb 2, 2020
  2. Deleted User

    Deleted User

    Guest

    The problem described at 2) in the post above also happens in 2020.1.0a18.

    I keep developing my game with 2019.2.17f1, where this problem does not happen.
     
    Last edited by a moderator: Jan 28, 2020
  3. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    3,136
    Could you please submit a bug report for this with the project attached? We'll need to reproduce the issue to understand what is going on.
     
  4. Deleted User

    Deleted User

    Guest

    Sending my entire project? 242mb? Err, no. I could send the project without the Library folder but the person who will be in charge:
    • will not understand what I'm talking about and send me to a bug threat not related at all,
    • will not experience the problem,
    and the bug report will be closed. That's always how things happen.

    Do I sound bitter? Well, I am. I must find a way to replace the code by something else so that it works in 2019.3 and 2020.1.

    I haven't upgraded my project to 2019.3.0f6 yet; I can try and see if the problem is still there but I'm not confident at all.

    Thanks for answering anyway! :)
     
  5. Deleted User

    Deleted User

    Guest

    @LeonhardP I just did it and the problem is there too.

    I built the game, in case, and the problem is also in the built game.
     
  6. Deleted User

    Deleted User

    Guest

    @LeonhardP I've found a better way to describe the problem in a few words. It appears that in the code below:
    Code (CSharp):
    1.             if(playersInput.jumpPressed)
    2.             {
    3.                 isHanging = false;
    4.                 rigidBody.bodyType = RigidbodyType2D.Dynamic;
    5.                 rigidBody.AddForce(new Vector2(0f, hangingJumpForce), ForceMode2D.Impulse);
    6.                 return;
    7.             }
    ForceMode2D.Impulse doesn't fire and the player falls instead of jumping.

    The weirdest still being that this happens at precise places only (I tested countless times and it always happens at these places in that scene).

    The problem also happens on procedurally generated scenes; for obvious reasons I cannot check if it happens always at the same place. :)
     

    Attached Files:

    Last edited by a moderator: Feb 3, 2020
  7. rz_0lento

    rz_0lento

    Joined:
    Oct 8, 2013
    Posts:
    2,361
    ForceMode2D is just enum and 0 happened to be the index for that Force.

    As for the thing breaking, have you verified by Debug.Log etc that the values you feed to rigidBody.AddForce have meaningful content when it fails?
     
  8. Deleted User

    Deleted User

    Guest

    I haven't noticed any difference between when it works and when it doesn't. Or, it's a very elusive difference...
     
  9. rz_0lento

    rz_0lento

    Joined:
    Oct 8, 2013
    Posts:
    2,361
    What I try to say, it's better to just log what happens instead of trying to visually make sense to it. It's close to impossible to debug things properly if you only got visuals to look at :)
     
  10. Deleted User

    Deleted User

    Guest

    True but why does the problem happen in 2019.3 and 2020.1 and not in 2019.2, and systematically at exactly the same places? Why, all of a sudden, would conditions to be false in some versions and not in the other?
     
  11. rz_0lento

    rz_0lento

    Joined:
    Oct 8, 2013
    Posts:
    2,361
    People really can't to tell what's going on without repro project / someone debugging your code (which ideally would be you).

    There could be some other change that breaks it and if you don't debug it, it's impossible to tell for sure that it's the physics engine to blame.
     
  12. Deleted User

    Deleted User

    Guest

    So, I added some Debug.Log messages to the code and tried it. The code with the debug lines is below. According to my understanding, if the "rigidBody.AddForce(new Vector2(0f, hangingJumpForce), ForceMode2D.Impulse);" didn't fire, the Debug.Log line that is just under it should not display; am I right or am I overlooking something?
    Code (CSharp):
    1. if(playersInput.jumpPressed)
    2.             {
    3.                 Debug.Log("jumpPressed is " + playersInput.jumpPressed);
    4.                 isHanging = false;
    5.                 Debug.Log("isHanging is " + isHanging);
    6.                 rigidBody.bodyType = RigidbodyType2D.Dynamic;
    7.                 Debug.Log("rigidBody.bodyType is " + rigidBody.bodyType);
    8.                 rigidBody.AddForce(new Vector2(0f, hangingJumpForce), ForceMode2D.Impulse);
    9.                 Debug.Log("Player is hanging and jumping!");
    10.                 return;
    11.             }
    Then I started the game, tested the code at places where I know everything will work properly and then at one place where I know the code (or anything else) will fail. Three times I had my player hang at the platform and pressed the Jump button (space bar). Three times the player fell on the floor instead of jumping but according to the console everything works:

    Sans-titre-1.jpg
     
  13. rz_0lento

    rz_0lento

    Joined:
    Oct 8, 2013
    Posts:
    2,361
    You could check that the hangingJumpForce is still the same (if you calculate it at runtime that is). But if you get that issue when against certain geometry in the scene, I'd check that there really isn't anything blocking the way. You could do
    OnCollisionEnter2D on your character and see if it hits something when you jump etc. Or try to debug the physics colliders somehow (I haven't used Unity's 2D physics so I don't if they have visualizer for physics shapes or not).

    It doesn't sound like the AddForce itself is somehow failing to execute but rather can't do visually what you expect it to do.
     
  14. Deleted User

    Deleted User

    Guest

    It is; it's a publv float.

    I have modified the Debug.Log for the last line of code so that it's more precise, into:
    Code (CSharp):
    1. rigidBody.AddForce(new Vector2(0f, hangingJumpForce), ForceMode2D.Impulse);
    2. Debug.Log("AddForce = " + (new Vector2(0f, hangingJumpForce), ForceMode2D.Impulse));
    and the result in the console is:
    Code (CSharp):
    1. AddForce = ((0.0, 11.0), Impulse)
     
  15. Deleted User

    Deleted User

    Guest

    Okay, I'm going to keep that problem and make it a "feature". I'll pretend that the player just fails jumping when she or he is hanging.

    There is already another way for them to get out of this situation and I'm going to work out others in addition.