Get coordinates of object mouse is on

I am trying to create grid based movement on a top down game that would be controlled with the cursor and in my code i need to get the transform.position of the object the mouse is on, how would i do this ? (If you have any other way I could make this movement i would appreciate it aswell)

This is part of a script that I am using in my game and I have this on my character, but this makes it so when you right click it shows the coordinates of where you clicked the mouse.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NAME : MonoBehaviour
{
 

    Camera cam;
    Vector3 startPoint;
    Vector3 endPoint;


    public void Start()
    {
        cam = Camera.main;
    }   

    public void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            startPoint = cam.ScreenToWorldPoint(Input.mousePosition);
            startPoint.z = 15;
            Debug.Log(startPoint);
        }

        if (Input.GetMouseButtonUp(0))
        {
            endPoint = cam.ScreenToWorldPoint(Input.mousePosition);
            endPoint.z = 15;
        }
    }
}