From b6535a62f9f3b2a39945268c408f406327623fbe Mon Sep 17 00:00:00 2001 From: BOT Alex <44818698+MagicBOTAlex@users.noreply.github.com> Date: Sat, 20 Apr 2024 18:04:55 +0200 Subject: [PATCH] Slots: On hover not works --- .../PlacementSystem/CameraSlotClickDetect.cs | 32 +++++++++++++++++-- .../PlacementSystem/PlacementManager.cs | 1 + Assets/Scripts/PlacementSystem/SlotManager.cs | 13 ++++++++ 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/Assets/Scripts/PlacementSystem/CameraSlotClickDetect.cs b/Assets/Scripts/PlacementSystem/CameraSlotClickDetect.cs index bb24fe4..f505783 100644 --- a/Assets/Scripts/PlacementSystem/CameraSlotClickDetect.cs +++ b/Assets/Scripts/PlacementSystem/CameraSlotClickDetect.cs @@ -1,5 +1,6 @@ using System.Collections; using System.Collections.Generic; +using Unity.VisualScripting; using UnityEngine; public class CameraSlotClickDetect : MonoBehaviour @@ -7,15 +8,19 @@ public class CameraSlotClickDetect : MonoBehaviour public Camera mainCamera; public LayerMask layerMask; + private SlotManager PrevHoveredSlot; + void Update() { if (Input.GetMouseButtonDown(0)) { - ShootRay(); + ShootClickRay(); } + + ShootHoverRay(); } - void ShootRay() + void ShootClickRay() { Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); RaycastHit hit; @@ -24,8 +29,29 @@ public class CameraSlotClickDetect : MonoBehaviour { var slotInfo = hit.collider.gameObject.GetComponentInParent(); slotInfo.OnClick(); + } + } - Debug.Log($"Hit PlacementSlot! At ({slotInfo.x}, {slotInfo.y})"); + 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) + { + slotInfo.OnHovered(); + PrevHoveredSlot.OnUnHovered(); + } + + PrevHoveredSlot = slotInfo; + } + else + { + PrevHoveredSlot = null; } } } diff --git a/Assets/Scripts/PlacementSystem/PlacementManager.cs b/Assets/Scripts/PlacementSystem/PlacementManager.cs index a6f3688..610efce 100644 --- a/Assets/Scripts/PlacementSystem/PlacementManager.cs +++ b/Assets/Scripts/PlacementSystem/PlacementManager.cs @@ -67,6 +67,7 @@ public class TowerPlacementManager : MonoBehaviour } + [Space(10)] public Material SilhouetteMaterial; public GameObject ToSilhouette(GameObject obj) diff --git a/Assets/Scripts/PlacementSystem/SlotManager.cs b/Assets/Scripts/PlacementSystem/SlotManager.cs index d52496e..fef6461 100644 --- a/Assets/Scripts/PlacementSystem/SlotManager.cs +++ b/Assets/Scripts/PlacementSystem/SlotManager.cs @@ -16,8 +16,21 @@ public class SlotManager : MonoBehaviour /// public event Action OnSlotClicked; + public event Action OnSlotHovered; + public event Action OnSlotUnHovered; + public void OnClick() { OnSlotClicked?.Invoke(this, spawnerRef, x, y); } + + public void OnHovered() + { + OnSlotHovered?.Invoke(this, spawnerRef, x, y); + } + + public void OnUnHovered() + { + OnSlotUnHovered?.Invoke(this, spawnerRef, x, y); + } }