Last active
August 6, 2020 19:15
-
-
Save AxizY/971ff9985be5a444d9e13248a1aff68b to your computer and use it in GitHub Desktop.
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
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class BlockPlace : MonoBehaviour | |
{ | |
public GameObject tile; //sprite you want to use (for me it is a 1x1 which has 1 pixel per unity) | |
public Camera cam; // main camera | |
private List<GameObject> alrPlaced; | |
public GameObject tilesHolder; //empty gameobject that holds all of the tiles as children so the hierarchy isnt crazy | |
void Update() | |
{ | |
if (Input.GetMouseButtonDown(0)) | |
{ | |
GameObject placed = Instantiate(tile, new Vector3(Mathf.Round(cam.ScreenToWorldPoint(Input.mousePosition).x /*.5f is the size of the tile im using*/.5f) * .5f, Mathf.Round(cam.ScreenToWorldPoint(Input.mousePosition).y / .5f) * .5f, 10), Quaternion.identity); | |
bool alreadyPlaced = false; | |
foreach (GameObject a in alrPlaced) | |
{ | |
if (placed.transform.position == a.transform.position) | |
{ | |
alreadyPlaced = true; | |
} | |
} | |
if (alreadyPlaced == true) | |
{ | |
Destroy(placed); | |
} else | |
{ | |
alrPlaced.Add(placed); | |
placed.transform.parent = tilesHolder.transform; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment