How to make something face towards the mouse pointer?

Hello! Sooo, I’m making a top-down shooter, with a control scheme similar to the game Hotline Miami, and I’m having trouble getting the character to face towards the mouse pointer. I tried using Ray Casting, but when I do that, the character spazzes out if I move the mouse underneath him, or on top of a wall. Here’s the script:

var lookTarget : Vector3;
 
function Update() 
{
    var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    var hit : RaycastHit;
    if (Physics.Raycast (ray, hit)) 
    {
        lookTarget = hit.point;
    }
 
    transform.LookAt(lookTarget);
}

How can I make it so the character ONLY rotates along the Y axis? Thanks for the help.

You don’t need Raycast. I assume with top down you mean the camera is positioned directly above the player on the +Y axis, in which case something like this should work (untested as I don’t have Unity open):

Vector3 lookAt = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, (transform.position - Camera.main.transform.position).magnitude));
lookAt.y = transform.position.y;  // Set it to the players height to avoid any non-y rotations
transform.LookAt(lookAt);