NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/Utils/GroundHitHelper.cs

29 lines
843 B
C#
Raw Normal View History

2023-08-22 19:08:45 +08:00
using Gameplay.Level;
2023-12-29 15:07:04 +08:00
using TGS;
namespace Gameplay
2023-08-22 19:08:45 +08:00
{
using UnityEngine;
2023-12-29 15:07:04 +08:00
2023-10-19 15:00:19 +08:00
public static class GroundHitHelper
2023-08-22 19:08:45 +08:00
{
private static RaycastHit[] _raycastHits = new RaycastHit[10];
2023-12-29 15:07:04 +08:00
public static int CalcHitCellIndex(Vector2 mousePose, TerrainGridSystem tgs)
2023-08-22 19:08:45 +08:00
{
var camera = Camera.main;
var ray = camera.ScreenPointToRay(mousePose);
var layerMask = LayerMask.GetMask("Ground");
var raycastCount = Physics.RaycastNonAlloc(ray, _raycastHits, 1000, layerMask);
if (raycastCount > 0)
{
var firstHit = _raycastHits[0];
var hitPoint = firstHit.point;
2023-12-29 15:07:04 +08:00
var cell = tgs.CellGetAtPosition(hitPoint, true);
return cell?.index ?? -1;
2023-08-22 19:08:45 +08:00
}
2023-12-29 15:07:04 +08:00
2023-08-22 19:08:45 +08:00
return -1;
}
}
2023-12-29 15:07:04 +08:00
}