Search Unity

2D collider geometry still overlapping after collision

Discussion in 'Physics' started by jjfawkes, Sep 22, 2019.

  1. jjfawkes

    jjfawkes

    Joined:
    May 28, 2013
    Posts:
    3
    Hi,
    My character sprite doesn't stop when it collides with a static collider - it crashes into it and then it is slowly pushed out of it.
    Here's a GIF of how it looks like:
    https://i.imgur.com/URJhBeX.gifv

    My setup is the following:
    1) Walls have Tilemap Collider 2d, which is used by Composite Collider 2D and it also has a RigidBody 2d.
    2) Player is a game object which has a Capsule Collider 2D & RigidBody 2D. Player also has a nested Sprite Renderer.
    3) Player is controlled with the following script:

    Code (CSharp):
    1.      
    2.     void FixedUpdate()
    3.     {
    4.         var vertical = Input.GetAxis("Vertical") * speed;
    5.         var horizontal = Input.GetAxis("Horizontal") * speed;
    6.  
    7.         rb2d.velocity = new Vector2(Mathf.Lerp(0, horizontal, 0.8f),
    8.             Mathf.Lerp(0, vertical, 0.8f));
    9.     }
    10.  
    I also tried using rb2d.MovePosition, but it has the same issue.

    I would like the player to completely stop as soon as it encounters the wall colliders, I don't like that it crashes into them and is bounced back.

    What am I doing wrong?