Hi all,
I have these scripts below…
[SerializeField]
private GameObject finalObject;
private Vector2 mousePos;
[SerializeField]
private LayerMask allTilesLayer;
void Update()
{
mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.position = new Vector2(Mathf.Round(mousePos.x), Mathf.Round(mousePos.y));
if (Input.GetMouseButtonDown(0))
{
RaycastHit2D rayHit = Physics2D.Raycast(transform.position, Vector2.zero, Mathf.Infinity, allTilesLayer);
if (rayHit.collider != null)
{
if (rayHit.collider.gameObject.tag == "Terrain" || gameObject.tag == "Object")
{
Instantiate(finalObject, transform.position, Quaternion.identity);
}
}
else if (rayHit.collider == null && gameObject.tag == "GrassTemplate")
{
Instantiate(finalObject, transform.position, Quaternion.identity);
}
}
}
private int selectedObjectInArray;
private GameObject currentlySelectedObject;
[SerializeField]
private GameObject[] selectableObjects;
private bool isAnObjectSelected = false;
void Start()
{
selectedObjectInArray = 0;
}
void Update()
{
Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 spawnPos = new Vector2(Mathf.Round(mousePos.x), Mathf.Round(mousePos.y));
if (Input.GetKeyDown(KeyCode.E) && isAnObjectSelected == false)
{
currentlySelectedObject = Instantiate(selectableObjects[selectedObjectInArray], spawnPos, Quaternion.identity);
isAnObjectSelected = true;
}
if (Input.GetMouseButtonDown(1) && isAnObjectSelected == true)
{
Destroy(currentlySelectedObject);
isAnObjectSelected = false;
selectedObjectInArray = 0;
}
}
…which places objects at the mouse position no problem but because I am using a isometric view the placement leaves gaps between the sprite as per image below.
Any help would be much appreciated, thank you!