Skip to content

Instantly share code, notes, and snippets.

@cracken56
Created March 18, 2020 12:39
Show Gist options
  • Save cracken56/6f3475a65917173798bf8133c1c300bf to your computer and use it in GitHub Desktop.
Save cracken56/6f3475a65917173798bf8133c1c300bf to your computer and use it in GitHub Desktop.
Tilemap Collider Script
public class TilemapScript : MonoBehaviour
{
Tilemap tilemap;
Tile tileGreen;
Tile tileBlack;
TileBase tileCheck;
private void Awake()
{
tileGreen = Resources.Load<Tile>("Green") as Tile;
tileBlack = Resources.Load<Tile>("Black") as Tile;
}
// Start is called before the first frame update
void Start()
{
tilemap = GetComponent<Tilemap>();
}
// CHANGE TILES ACCORDING TO COLLISIONS
void OnCollisionEnter2D(Collision2D collision)
{
Vector3 hitPosition = Vector3.zero;
{
if (tilemap != null && Manager.Instance.go_Radar == collision.gameObject)
{
foreach (ContactPoint2D hit in collision.contacts)
{
hitPosition.x = hit.point.x;
hitPosition.y = hit.point.y;
hitPosition.z = 0;
tileCheck = tilemap.GetTile(tilemap.WorldToCell(hitPosition));
if (tileCheck == tileBlack && tileCheck != null)
{
tilemap.SetTile(tilemap.WorldToCell(hitPosition), tileGreen); // PROBLEM: IT ONLY TURNS GREEN THE FIRST TILES IT COMES IN CONTACT WITH.
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment