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(); 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(); if (tower != null) 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(); if (PrevHoveredSlot != null && PrevHoveredSlot != slotInfo) { PrevHoveredSlot.OnUnHovered(); } slotInfo.OnHovered(); PrevHoveredSlot = slotInfo; } else { PrevHoveredSlot = null; } } }