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

82 lines
2.4 KiB
C#
Raw Normal View History

2024-04-21 01:19:36 +02:00
using Cinemachine;
2024-04-20 01:51:12 +02:00
using UnityEngine;
public class CameraSlotClickDetect : MonoBehaviour
{
2024-04-21 02:09:30 +02:00
[SerializeField] private TowerCam towerCam;
2024-04-20 01:51:12 +02:00
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;
if (!GameManager.Instance.IsBuildMode && Physics.Raycast(ray, out selectHit, Mathf.Infinity, selectLayer))
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 02:09:30 +02:00
if (towerCam != null)
towerCam.ChangeToTarget(tower.gameObject);
else
Debug.LogWarning("TowerCam scpritet is not assigned, assign in the inspector");
2024-04-21 00:38:10 +02:00
}
}
else if (GameManager.Instance.IsBuildMode && Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask))
2024-04-21 00:38:10 +02:00
{
var slotInfo = hit.collider.gameObject.GetComponentInParent<SlotManager>();
slotInfo.OnClick();
GameManager.Instance.SelectedTower = null;
2024-04-21 02:09:30 +02:00
if (towerCam != null)
towerCam.ChangeToTarget(null);
else
Debug.LogWarning("TowerCam scpritet is not assigned, assign in the inspector");
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
if (GameManager.Instance.IsBuildMode)
if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask))
{
var slotInfo = hit.collider.gameObject.GetComponentInParent<SlotManager>();
2024-04-20 18:04:55 +02:00
if (PrevHoveredSlot != null && PrevHoveredSlot != slotInfo)
{
PrevHoveredSlot.OnUnHovered();
}
slotInfo.OnHovered();
PrevHoveredSlot = slotInfo;
}
else
2024-04-20 18:04:55 +02:00
{
if (PrevHoveredSlot != null)
PrevHoveredSlot.OnUnHovered();
PrevHoveredSlot = null;
2024-04-20 18:04:55 +02:00
}
2024-04-20 01:51:12 +02:00
}
}