Files
CityBuilder/Assets/Scripts/Selector.cs
SHOUTING_PIRATE 1c91215efd initial commit
2025-07-07 20:59:04 +01:00

43 lines
1.3 KiB
C#

using UnityEngine;
using UnityEngine.EventSystems;
public class Selector : MonoBehaviour
{
public static Selector Instance { get; private set; }
private void Awake()
{
Instance = this;
}
private Camera cam;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
cam = Camera.main;
}
//returns the tile that the mouse is currently over
public Vector3 GetCurTilePosition()
{
//return if hovering over UI
if (EventSystem.current.IsPointerOverGameObject())
{
return new Vector3(0, -99, 0);
}
//create the plane, ray and out distance
Plane plane = new Plane(Vector3.up, Vector3.zero);
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
float rayOut = 0.0f;
//shoot the ray against the plane
if (plane.Raycast(ray, out rayOut))
{
//get the position at which we intersected the plane
Vector3 newPos = ray.GetPoint(rayOut) - new Vector3(0.5f, 0.0f, 0.5f);
//round the position to the nearest tile
newPos = new Vector3(Mathf.CeilToInt(newPos.x), 0.0f, Mathf.CeilToInt(newPos.z));
//return the new position
return newPos;
}
return new Vector3(0, -99, 0);
}
}