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. Dismiss Notice

Resolved (FIXED) Collision while running?

Discussion in '2D' started by blade_runner82, Aug 25, 2020.

  1. blade_runner82

    blade_runner82

    Joined:
    Aug 17, 2020
    Posts:
    4
    [FIXED] Fix is at comments

    Hi everyone, my first post here. Sorry for any mistakes I might make.

    I am currently learning unity and trying to make a little platformer. I have my movement functions and ground checker functions below. The problem is, when I'm moving left or right, the player character randomly collides with something and it stops in its tracks. It can move again if I press the left or right buttons but this is pretty annoying and is a problem in the project.

    I have BoxCollider2D on my player model
    and TilemapCollider on my tileset layer, which holds the tiles.

    Thanks in advance.

    My box collider shape on the sprite:



    My movement function:

    Code (CSharp):
    1.  public void Move()
    2.     {
    3.         float x = Input.GetAxis("Horizontal");
    4.         float moveBy = x * speed;
    5.         rb.velocity = new Vector2(moveBy, rb.velocity.y);
    6.         if (x > 0f)
    7.         {
    8.             transform.localScale = new Vector2(1, 1);
    9.             state = State.running;
    10.          
    11.         }
    12.         else if (x < 0f)
    13.         {
    14.             transform.localScale = new Vector2(-1, 1);
    15.             state = State.running;
    16.        
    17.         }
    18.  
    19.  
    20.     }
    21.  
    My groundcheck function:

    Code (CSharp):
    1.  public void CheckIfGrounded()
    2.     {
    3.         Collider2D collider = Physics2D.OverlapCircle(isGroundChecker.position, checkGroundRadius, groundLayer);
    4.  
    5.         if (collider != null)
    6.         {
    7.             isGrounded = true;
    8.         }
    9.         else
    10.         {
    11.             isGrounded = false;
    12.         }
    13.     }
     
    Last edited: Aug 29, 2020
  2. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
    Maybe friction on the tile colliders?
     
  3. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    The edges of the BoxCollider2D are likely getting caught on any gaps between other tile colliders for your ground.
    Character objects are typically given a CapsuleCollider2D instead to prevent this, since the rounded ends allow the collider to slide over small gaps and also move up/down hills or other inclines smoothly.
     
  4. blade_runner82

    blade_runner82

    Joined:
    Aug 17, 2020
    Posts:
    4
    Thanks for the replies,
    I managed to fix it by removing
    Code (CSharp):
    1. TilemapCollider2D
    from the tileset and adding
    Code (CSharp):
    1. EdgeCollider2D
    . Also, I created a
    Code (CSharp):
    1. PhysicsMaterial2D
    2.  
    and set its friction to 0 and added it to the material of the playermodel.
    Now its working fine. Another issue I had was the player would get stuck to the walls and would not go down unless I would let go of the movement keys. Both of these issues are now solved.