Created
March 18, 2020 12:39
-
-
Save cracken56/6f3475a65917173798bf8133c1c300bf to your computer and use it in GitHub Desktop.
Tilemap Collider Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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