1
Fork 0
mirror of https://github.com/Steffo99/beat-td.git synced 2024-11-22 15:24:18 +00:00
beat-td/Assets/Scripts/TowerPlacer.cs

50 lines
1.5 KiB
C#
Raw Normal View History

2018-04-21 12:09:09 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TowerPlacer : MonoBehaviour
{
public GameObject selected = null;
SpriteRenderer cursorSprite;
2018-04-22 12:07:55 +00:00
SpriteRenderer towerGhost;
2018-04-21 12:09:09 +00:00
void Start()
{
2018-04-22 12:07:55 +00:00
SpriteRenderer[] spriteRenderers = gameObject.GetComponentsInChildren<SpriteRenderer>();
foreach (SpriteRenderer spriteRenderer in spriteRenderers)
{
if (spriteRenderer.gameObject == gameObject)
{
cursorSprite = spriteRenderer;
continue;
}
towerGhost = spriteRenderer;
}
2018-04-21 12:09:09 +00:00
}
void Update () {
//Check if there's nothing else under the cursor
Collider2D collider = Physics2D.OverlapPoint(transform.position);
if (collider == null)
{
cursorSprite.color = Color.white;
2018-04-22 12:07:55 +00:00
towerGhost.color = Color.white;
2018-04-21 12:09:09 +00:00
}
else
{
cursorSprite.color = Color.red;
2018-04-22 12:07:55 +00:00
towerGhost.color = new Color(1, 1, 1, 0);
2018-04-21 12:09:09 +00:00
}
if (Input.GetMouseButtonDown(0))
{
//Check if something is selected
if (selected == null) return;
//Ensure there is nothing below
if (collider != null) return;
//Place the item
Vector3 position = new Vector3(transform.position.x, transform.position.y, 0);
Instantiate(selected, position, transform.rotation);
}
}
}