Distance mouse drag bug

I have a sphere gameobject, the X and Z axis are locked, so I can only move the gameobject up and down on the Y. A script is attached to the gameobject that allows me to click and drag it up and down.

the z and x axis constraints are not in code but on the gameobject itself with a component called, " position constraint"

The problem is when I hold the gameobject and move far or closer, the gameobject moves a lot faster and not the speed of the player looking up and down. I understand the issue is the X and Z axis being locked is causing this, but I’m having a hard time finding a way to fix it without unlocking them.

Here is the script attached to the gameobject:

using UnityEngine;
using System.Collections;
using Unity.VisualScripting;

[RequireComponent(typeof(MeshCollider))]

public class TestLaser : MonoBehaviour {

    [Header("References")]
    [SerializeField] private Laser laser;
    private Vector3 screenPoint;
    private Vector3 offset;
    private float minX = -13f;
    private float maxX = 13f;
    private float minY = -6f;
    private float maxY = 6f;

    private void Update() {

        //clamping
        transform.position = new Vector3(Mathf.Clamp(transform.position.x, minX, maxX), Mathf.Clamp(transform.position.y, minY, maxY));

    }


    void OnMouseDown() {

            screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);

        offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
    }

    void OnMouseDrag() {

            Vector3 curScreenPoint = new Vector3((Input.mousePosition.x), Input.mousePosition.y, screenPoint.z);

            Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
        transform.position = curPosition;

    }



}

Your code works for me, Only thing wrong with the code is your trying to update the x position which I’m assuming your using a Rigidbody to lock the object constraints, What this does is makes the physics engine have a heart attack can you change your OnMouseDrag see if that helps.

void OnMouseDrag ()
{
    Vector3 curScreenPoint = new Vector3((transform.position.x), Input.mousePosition.y, screenPoint.z);
    Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
    transform.position = new Vector3(curPosition.x, curPosition.y, curPosition.z);
}