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

58 lines
1.3 KiB
C#
Raw Normal View History

2024-04-20 01:51:12 +02:00
using System.Collections;
using System.Collections.Generic;
2024-04-20 18:04:55 +02:00
using Unity.VisualScripting;
2024-04-20 01:51:12 +02:00
using UnityEngine;
public class CameraSlotClickDetect : MonoBehaviour
{
public Camera mainCamera;
public LayerMask layerMask;
2024-04-20 18:04:55 +02:00
private SlotManager PrevHoveredSlot;
2024-04-20 01:51:12 +02:00
void Update()
{
if (Input.GetMouseButtonDown(0))
{
2024-04-20 18:04:55 +02:00
ShootClickRay();
2024-04-20 01:51:12 +02:00
}
2024-04-20 18:04:55 +02:00
ShootHoverRay();
2024-04-20 01:51:12 +02:00
}
2024-04-20 18:04:55 +02:00
void ShootClickRay()
2024-04-20 01:51:12 +02:00
{
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask))
{
var slotInfo = hit.collider.gameObject.GetComponentInParent<SlotManager>();
2024-04-20 15:38:21 +02:00
slotInfo.OnClick();
2024-04-20 18:04:55 +02:00
}
}
void ShootHoverRay()
{
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
2024-04-20 01:51:12 +02:00
2024-04-20 18:04:55 +02:00
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;
2024-04-20 01:51:12 +02:00
}
}
}