title: Unity 2D collision detection with kinematic rigidbody
body: <p>Just starting out with Unity so please be patient :) We are building a Zelda like game - top down, no physics / gravity, tilemaps for the map. We don't want to use the dynamic rigidbodies because of all the physics behind it.</p>
  
  <p>The tilemap has "Walls" with a CompositeCollider2D and Static rigidbody, our player has a CircleCollider2D and a kinematic rigidbody. Both the player and the walls have a physics material with zero friction.</p>
  
  <p>What is the best way for the collision detection with a kinematic rigibody? We have several concepts like Physics2D.CircleCastAll but ended up with our player stuck in the walls or getting stuck at the corners (the player should move smoothly around the corner) or getting stuck in two overlapping colliders.</p>
  
  <p>The best solution we came up with is this (and the player still runs into the colliders sometimes but at least don't get stuck anymore).</p>
  
  <pre><code>// MoveDirection is the movement input
  movement = MoveDirection * Speed * Time.deltaTime;
  
  List&lt;RaycastHit2D&gt; results = new List&lt;RaycastHit2D&gt;();
  List&lt;RaycastHit2D&gt; resultsX = new List&lt;RaycastHit2D&gt;();
  List&lt;RaycastHit2D&gt; resultsY = new List&lt;RaycastHit2D&gt;();
  
  if (movement.x != 0)
  {
      actorCollider.Cast(new Vector2(movement.x, 0), filter, resultsX, movement.magnitude);
      results.AddRange(resultsX);
  }
  
  if (movement.y != 0)
  {
      actorCollider.Cast(new Vector2(0, movement.y), filter, resultsY, movement.magnitude);
      results.AddRange(resultsY);
  }
  
  foreach (RaycastHit2D hit in results)
  {
      Vector2 normal = hit.normal;
  
      float projection = Vector2.Dot(movement, normal);
  
      if (projection &lt; 0)
      {
          if (hit.distance &gt; 0)
          {
              projection += Mathf.Max(hit.distance - 0.01f, 0);
          }
  
          movement -= projection * normal;
      }
  }
  
  // move player
  newPosition = body.position + movement;
  
  // make sure the player is positioned at pixel perfect positions
  newPosition.x = (Mathf.Round(newPosition.x * 16) / 16);
  newPosition.y = (Mathf.Round(newPosition.y * 16) / 16);
  body.MovePosition(newPosition);
  
  // this part moves the player back in case it is in a collider.
  List&lt;Collider2D&gt; colliderList = new List&lt;Collider2D&gt;();
  if (actorCollider.OverlapCollider(filter, colliderList) &gt; 0)
  {
      foreach (Collider2D hit in colliderList)
      {
          var distance = hit.Distance(actorCollider);
          body.MovePosition(body.position + (distance.normal * Mathf.Abs(distance.distance)));
      }
  }
  </code></pre>
  
  <p>Player setup:</p>
  
  <p><a href="https://i.stack.imgur.com/GmcoW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GmcoW.png" alt="enter image description here"></a></p>
  
  <p>Walls setup:</p>
  
  <p><a href="https://i.stack.imgur.com/bpJ7j.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bpJ7j.png" alt="enter image description here"></a></p>
  
  <p>Somehow it still feels way too complicated. Is this the best approach to do the collision detections or do we do it completely wrong and there are better / easier ways? Keep in mind we want to be able to walk around the straight corners somehow and not get stuck on them. And there are overlapping colliders or colliders next to each other.</p>
  
  <p>Thank you for your time!</p>