Cursor and mouse position alignment

please format code with </> button * homework policy * asking questions

I am trying to highlight tiles, that are hovered by the mouse. This is my code:

void Update ()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
    Vector3 point = hit.point;
    Vector2Int gridPoint = Geometry.GridFromPoint(point);

    tileHighlight.SetActive(true);
    tileHighlight.transform.position = Geometry.PointFromGrid(gridPoint);
    if (Input.GetMouseButtonDown(0))
    {
      // ... 

Edit:

 public class Geometry
 {  
 static public Vector3 
  PointFromGrid(Vector2Int gridPoint)
{
    float x = -3.5f + 1.0f * gridPoint.x;
    float z = -3.5f + 1.0f * gridPoint.y;
    return new Vector3(x, 0, z);
}

static public Vector2Int GridPoint(int col, int row)
{
    return new Vector2Int(col, row);
}

static public Vector2Int GridFromPoint(Vector3 point)
{
    int col = Mathf.FloorToInt(4.0f + point.x);
    int row = Mathf.FloorToInt(4.0f + point.z);
    return new Vector2Int(col, row);
}

}
The cursor object is not under the mouse how can I fix and align it?

I just noticed that this looks like C#/Unity :exploding_head: I’m very sorry but this forum is for Processing / p5.js so I recommend you to ask on Unity forum.

2 Likes