Need help with mouse/touch control restriction.

Hi, I have a control issue with my 2D game.

Im looking to control a game object with either a mouse or touch vertically only.

at the moment I have this code:

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

public class PlayerMovement : MonoBehaviour {
float distance = 0f;

private void OnMouseDown()
{
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    distance = Vector3.Distance(ray.origin, transform.position);
}

private void OnMouseDrag()
{
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    transform.position = ray.origin + ray.direction * distance;
}

}

When I test the controls, I can move the object around with my mouse but I can move it anywhere, I would like to move it only up and down. I looked into clamping… but I don’t know how to code that into this. Ahh! this is so frustrating… any help would be greatly appreciated.

Save as a variable (ray.origin + ray.direction * distance). For example:
var myVector = ray.origin + ray.direction * distance;

And take only the desired axis (example ‘y’)
transform.position.Set(transform.position.x, myVector.y, transform.position.z);

Or use Translate()

If (condition) transform.Translate(0, step, 0);