How to make a 3D tank turret follow the DIRECTION of mouse cursor?

Hello,

i’m trying to create a classic “mouse controls” for an arcade game in which the player is a tank.

He sees his tank from the back (not from the barrel) and i want to use the mouse cursor to establish only the direction where the turret has to rotate (without rotating the barrel up and down to follow the cursor position).

At the moment i have this code took from another answer, but this rotates along all axes.

public class MoveToMouse : MonoBehaviour
{
    //  e.g. 0.2 = slow, 0.8 = fast, 2 = very fast, Infinity = instant
    [Tooltip("If rotationSpeed == 0.5, then it takes 2 seconds to spin 180 degrees")]
    [SerializeField] [Range(0, 10)] float rotationSpeed = 0.5f;

    [Tooltip("If isInstant == true, then rotationalSpeed == Infinity")]  
    [SerializeField] bool isInstant = false;

    Camera _camera = null;  // cached because Camera.main is slow, so we only call it once.

    void Start()
    {
        _camera = Camera.main;
    }

    void Update()
    {
        Ray ray = _camera.ScreenPointToRay(Input.mousePosition);   
        Quaternion targetRotation = Quaternion.LookRotation(ray.direction);

        if (isInstant)
        {
            transform.rotation = targetRotation;
        }
        else
        {
            Quaternion currentRotation = transform.rotation;
            float angularDifference = Quaternion.Angle(currentRotation, targetRotation);

            // will always be positive (or zero)
            if (angularDifference > 0) transform.rotation = Quaternion.Slerp(
                                         currentRotation,
                                         targetRotation,
                                         (rotationSpeed * 180 * Time.deltaTime) / angularDifference
                                    );
            else transform.rotation = targetRotation;
        }
    }
}

Any help?

I managed to do it by modifying this piece of code:

Ray ray = _camera.ScreenPointToRay(Input.mousePosition);
// Copy the ray's direction
Vector3 mouseDirection = ray.direction;
// Constraint it to stay in the X/Z plane
mouseDirection.y = 0;
//Look for the constraint direction
Quaternion targetRotation = Quaternion.LookRotation(mouseDirection);