Collision problems

Hey, im a complete newbe woking in 2d with a grid.
i have the player moving from tile to tile in all directions with a 2d box collider and a rigedbody cinetic.
i have a tilemap with tilemap collider.
The player should not go through the tilemap but he can.
When i set the player rigedbody to dynamic, it collides.

I dont know why that is, so thats why im here. I only have on code so far: for the player

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public float gridSize = 1f;
    private Vector3 targetPosition;
    private bool isMoving = false;

    private void Update()
    {
        if (!isMoving)
        {
        
            if (Input.GetKeyDown(KeyCode.UpArrow))
            {
                TryMove(Vector3.up);
            }
            else if (Input.GetKeyDown(KeyCode.DownArrow))
            {
                TryMove(Vector3.down);
            }
            else if (Input.GetKeyDown(KeyCode.LeftArrow))
            {
                TryMove(Vector3.left);
            }
            else if (Input.GetKeyDown(KeyCode.RightArrow))
            {
                TryMove(Vector3.right);
            }
        }
    }

    private void TryMove(Vector3 direction)
    {
      
        targetPosition = transform.position + direction * gridSize;

     
        if (IsValidMove(targetPosition))
        {
   
            StartCoroutine(MoveToTarget());
        }
    }

    private bool IsValidMove(Vector3 position)
    {
     
     

   
    }

    private System.Collections.IEnumerator MoveToTarget()
    {
        isMoving = true;

        while (Vector3.Distance(transform.position, targetPosition) > 0.01f)
        {
        
            transform.position = Vector3.MoveTowards(transform.position, targetPosition, Time.deltaTime * 5f);
            yield return null;
        }

     
        transform.position = targetPosition;
        isMoving = false;
    }
}
Thanks for any help

So what do you want to happen? This is what happens when you set your rigidbody to kinematic: Unity - Scripting API: Rigidbody.isKinematic (unity3d.com)

Another problem: You are using physics due to the rigidbody, but then you are directly manipulating the object’s transform. This will override your physics and cause all sorts of wonky behavior. Use Rigidbody.MovePosition, AddForce, or set the Velocity directly if you want to have physics.

I am guessing you want to have physics, so then you will want the rigidbody to not be Kinematic and then you will want to change your TryMove function away from changing the transform.position directly. The same goes for your Coroutine MoveToTarget.

If you’re moving stuff in a grid,

a) don’t use coroutines
b) don’t use physics

Instead, use the grid.

And in any case, as Corny pointed out, if you use ever physics, NEVER manipulate the transforms yourself because that bypasses physics.

Tile-based / grid-based 2D games: match3, tetris, chips challenge, rogue, etc:

For any tile-based game such as Match3 or Tetris or a grid-based Roguelike, do all the logical comparisons in your own data storage mechanism for the tiles, such as a 2D array of tiles.

Otherwise you needlessly bind your game logic into Unity objects and the Unity API, making it about 10x more complicated than it needs to be.

If you have no idea how to work with 2D arrays, hurry to some basic C# tutorials for the language portions of it, then look at any good tutorial that uses a 2D array

Here is my Match3 demo using this technique of storing data in a grid. Full source linked in game comments.

It stores all of its data in a 2D array:

PieceController[,] Board;

This allows for easy simple checking in code, not relying on anything like physics.

You should strive to use that pattern for all logic, then only use Unity to present what is happening in the game logic.