Slots: On hover not works

This commit is contained in:
BOT Alex 2024-04-20 18:04:55 +02:00
parent 3a73a81d66
commit b6535a62f9
3 changed files with 43 additions and 3 deletions

View File

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

View File

@ -67,6 +67,7 @@ public class TowerPlacementManager : MonoBehaviour
}
[Space(10)]
public Material SilhouetteMaterial;
public GameObject ToSilhouette(GameObject obj)

View File

@ -16,8 +16,21 @@ public class SlotManager : MonoBehaviour
/// </summary>
public event Action<SlotManager, GridManager, int, int> OnSlotClicked;
public event Action<SlotManager, GridManager, int, int> OnSlotHovered;
public event Action<SlotManager, GridManager, int, int> 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);
}
}