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

68 lines
1.8 KiB
C#
Raw Normal View History

2024-04-20 01:51:12 +02:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraSlotClickDetect : MonoBehaviour
{
public Camera mainCamera;
public LayerMask layerMask;
2024-04-20 22:19:23 +02:00
public LayerMask selectLayer;
2024-04-20 01:51:12 +02:00
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;
2024-04-20 22:19:23 +02:00
RaycastHit selectHit;
2024-04-20 22:42:06 +02:00
if (Physics.Raycast(ray, out selectHit, Mathf.Infinity, selectLayer) && !GameManager.Instance.IsBuildMode)
2024-04-20 22:19:23 +02:00
{
2024-04-20 22:42:06 +02:00
var tower = selectHit.collider.gameObject.GetComponentInChildren<Tower>();
2024-04-20 23:32:42 +02:00
if (tower != null)
2024-04-21 00:38:10 +02:00
{
2024-04-20 23:32:42 +02:00
GameManager.Instance.SelectedTower = tower;
2024-04-21 00:38:10 +02:00
}
}
else if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask))
{
var slotInfo = hit.collider.gameObject.GetComponentInParent<SlotManager>();
slotInfo.OnClick();
GameManager.Instance.SelectedTower = null;
2024-04-20 22:19:23 +02:00
}
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)
{
PrevHoveredSlot.OnUnHovered();
}
2024-04-20 19:22:54 +02:00
slotInfo.OnHovered();
2024-04-20 18:04:55 +02:00
PrevHoveredSlot = slotInfo;
}
else
{
PrevHoveredSlot = null;
2024-04-20 01:51:12 +02:00
}
}
}