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