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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Why RigidBody2D gains supernatural force when encountering an Edge Collider?

Discussion in '2D' started by IgorUnity3D, Aug 18, 2017.

  1. IgorUnity3D

    IgorUnity3D

    Joined:
    Aug 28, 2014
    Posts:
    66
    Hello,

    I have a RigidBody2D and while I'm moving it horizontally it gains a supernatural strength when it encounters some pointed collider .... why does this happen? I have tried to apply some settings in RigidBody2D but I did not succeed.

    What is wrong?



    Please see the video:



    Every help is welcome!
     
    Last edited: Aug 18, 2017
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    Would you mind posting the code that drives your character's movement? It would help a lot to figure out the source of the issue. Please use code tags.
     
  3. IgorUnity3D

    IgorUnity3D

    Joined:
    Aug 28, 2014
    Posts:
    66
    Hello @jeffreyschoch, see this please:

    Code (CSharp):
    1. private void Update()
    2.     {
    3.  
    4.         //Time.timeScale = 0.7f;
    5.  
    6.         GroundDetection();
    7.  
    8.         command = EnumPlayerCommands.Move;
    9.  
    10.         if (Input.GetKeyDown(KeyCode.Space) && _grounded)
    11.             command = EnumPlayerCommands.Jump;
    12.  
    13.         if (HORIZONTAL_AXIS > 0 && !_facingRight)
    14.             command = EnumPlayerCommands.Flip;
    15.         else if (HORIZONTAL_AXIS < 0 && _facingRight)
    16.             command = EnumPlayerCommands.Flip;
    17.  
    18.     }
    19.  
    20.     void FixedUpdate()
    21.     {
    22.         switch (command)
    23.         {
    24.             case EnumPlayerCommands.Move:
    25.                 MoveHorizontal();
    26.                 break;
    27.             case EnumPlayerCommands.Jump:
    28.                 Jump();
    29.                 break;
    30.             case EnumPlayerCommands.Flip:
    31.                 Flip();
    32.                 break;
    33.             default:
    34.                 break;
    35.         }
    36.  
    37.         HORIZONTAL_AXIS = Input.GetAxisRaw("Horizontal"); //-1,0,1
    38.  
    39.         animator.SetFloat("vertical_speed", rgb2d.velocity.y);
    40.     }
    41.  
    42.     void MoveHorizontal() {
    43.         var hVelocity = moveSpeedHorizontal * HORIZONTAL_AXIS;
    44.         rgb2d.velocity = new Vector2(hVelocity, rgb2d.velocity.y);
    45.         animator.SetFloat("horizontal_speed", Mathf.Abs(hVelocity));
    46.     }
    47.  
    48.     void Jump() {
    49.         rgb2d.AddForce(new Vector2(0, jumpForce));
    50.     }
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    Well, the solution doesn't immediately jump out to me, but some things to try:

    Move your horizontal input polling to Update, you want the inputs to be frame-accurate. You may also want to try using "rgb2d.AddForce(Vector2.right * hVelocity)" instead of setting velocity directly. You're probably overwriting any deceleration due to resistance or friction caused by the surface at a steep angle, possibly causing the upward motion as it's redirected each frame by the physics system correcting for collisions.
     
  5. IgorUnity3D

    IgorUnity3D

    Joined:
    Aug 28, 2014
    Posts:
    66
    Hey...

    I tried what you suggested, it seems to me that something has improved. But the momentum did not stop when I kept pressing against the collider.
    Code (CSharp):
    1.     void MoveHorizontal() {
    2.         var hVelocity = moveSpeedHorizontal * HORIZONTAL_AXIS;
    3.         //rgb2d.velocity = new Vector2(hVelocity, rgb2d.velocity.y);
    4.         rgb2d.AddForce(Vector2.right * hVelocity);
    5.         animator.SetFloat("horizontal_speed", Mathf.Abs(hVelocity));
    6.     }
    7.  
    8.     private void Update()
    9.     {
    10.  
    11.  
    12.         HORIZONTAL_AXIS = Input.GetAxisRaw("Horizontal"); //-1,0,1
    13.       ...
    14.     }
     
  6. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    Yeah, I would expect it to continue pushing, but at least now it will take into account deceleration. Is it possible that given your gravity and speed settings, it's simply using that angle as a ramp? If your goal is to keep the player on the ground at all times while running, you could do a raycast downwards and move the player to the ground, then stop raycasting during a jump until grounded again.
     
  7. IgorUnity3D

    IgorUnity3D

    Joined:
    Aug 28, 2014
    Posts:
    66
    Exactly ... at that point we not want the player to use it as a ramp.

    So I would have to do a function that keeps pulling him down while he walks?
     
  8. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    The physics system is designed to be as realistic as possible, so if you want something unrealistic like an object not carrying momentum off an incline, you'll need to code something to make that behavior happen.
     
  9. IgorUnity3D

    IgorUnity3D

    Joined:
    Aug 28, 2014
    Posts:
    66
    You're absolutely right. I'll think of something that solves this problem. Thank you very much for the clarification.

    One last help ... you think this problem: (https://forum.unity3d.com/threads/camera-smooth-player-moves-little-bit-back-when-jump.489271/) Does it also have to do with physics?