Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

2D Sticky Collision Problem

Discussion in '2D' started by Mails_PR, Feb 19, 2019.

  1. Mails_PR

    Mails_PR

    Joined:
    Jul 30, 2018
    Posts:
    9
    I am in the middle of learning how to create basic platformers and have recently learnt how to create character controllers. I have encountered a weird bug when doing so, however, that puzzles me and I have no idea how to fix it. When my character is in a free fall and encounters a wall on the left or on the right, if I hold the right or left arrow keys, the character can stick to the wall and stop falling. As soon as I release these keys, it stops sticking and continues falling. What could be the reason for that? Does that have something to with scripting or colliders?
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    2,014
    Maybe you're setting the velocity directly which stomps over the velocity changes that gravity applies?
     
  3. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    1,357
    Hi @Mails_PR

    You didn't quite explain how you move your object (physics system or just moving transform manually?).

    So I'm just guessing.

    It sounds just like you are using physics system to move your player.

    When you apply force sideways along x-axis, your Rigidbody will push against wall, and if that wall collider has friction it will stick to it like in reality if you would push some block against wall with your hand, it wouldn't fall down.

    So either stop adding force if touching a wall or make the wall have no or little friction.
     
  4. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    145
  5. Mails_PR

    Mails_PR

    Joined:
    Jul 30, 2018
    Posts:
    9
    Hello, MelvMay! Thank you for your response. I do set the velocity in FixedUpdate the following piece of code.
    Code (CSharp):
    1.  
    2.   void FixedUpdate ()
    3. {
    4.         if (alive && !stageComplete) {
    5.             grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
    6.             anim.SetBool("Ground", grounded);
    7.             anim.SetBool("Alive", alive);
    8.             anim.SetFloat ("vSpeed", rb.velocity.y);
    9.             float move = Input.GetAxis ("Horizontal");
    10.          
    11.             anim.SetFloat("Speed", Mathf.Abs(move));
    12.          
    13.             rb.velocity = new Vector2(move * maxSpeed, rb.velocity.y);
    14.             //GetComponent<Rigidbody2D>().
    15.             if (move > 0 && !facingRight)
    16.             {
    17.                 Flip();
    18.             }
    19.             else if (move < 0 && facingRight)
    20.             {
    21.                 Flip();
    22.             }
    23.         }
    24.     }
    It seems though that the problem was connected to friction since when I applied the method used in the video above, the character stopped sticking.

    Hello, eses! Thank you for your kind response. I am not using force but Input.GetAxis. I could attach the full script, if that would be of any help, since I really want to know the exact explanation of the problem. As said above, however, the video helped to solve the problem for now by decreasing friction to 0.

    Thanks for the video! It really helped :)
     
  6. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    1,357
    @Mails_PR

    "Hello, eses! Thank you for your kind response. I am not using force but Input.GetAxis." ..."It seems though that the problem was connected to friction since when I applied the method used in the video above, the character stopped sticking."


    ...well not sure about that video @cscox2191 posted but I already mentioned it mostly has to do with friction/physics movement (GetAxis is just input so it's irrelevant here). This is what I said:

    "When you apply force sideways along x-axis, your Rigidbody will push against wall, and if that wall collider has friction it will stick to it like in reality if you would push some block against wall with your hand, it wouldn't fall down.
    So either stop adding force if touching a wall or make the wall have no or little friction."


    So I'm not sure what part of my short answer you did read :)