3DTD/Assets/Scripts/PlacementSystem/CameraSlotClickDetect.cs

65 lines
1.7 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraSlotClickDetect : MonoBehaviour
{
public Camera mainCamera;
public LayerMask layerMask;
public LayerMask selectLayer;
private SlotManager PrevHoveredSlot;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
ShootClickRay();
}
ShootHoverRay();
}
void ShootClickRay()
{
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask))
{
var slotInfo = hit.collider.gameObject.GetComponentInParent<SlotManager>();
slotInfo.OnClick();
GameManager.Instance.SelectedTower = null;
}
RaycastHit selectHit;
if (Physics.Raycast(ray, out selectHit, Mathf.Infinity, selectLayer) && !GameManager.Instance.IsBuildMode)
{
var tower = selectHit.collider.gameObject.GetComponentInChildren<Tower>();
GameManager.Instance.SelectedTower = tower;
}
}
void ShootHoverRay()
{
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask))
{
var slotInfo = hit.collider.gameObject.GetComponentInParent<SlotManager>();
if (PrevHoveredSlot != null && PrevHoveredSlot != slotInfo)
{
PrevHoveredSlot.OnUnHovered();
}
slotInfo.OnHovered();
PrevHoveredSlot = slotInfo;
}
else
{
PrevHoveredSlot = null;
}
}
}