[Solved] How to move a character on the x-axis only by dragging your mouse or finger on a button?

Hello everyone,

Hopefully, some of you have worked out a solution for this problem in the past. I think this question is self-explanatory; however, I will give you some more details along with the code that I have so far. I want my gameObject to move along the x-axis when I drag my mouse or finger along a button that spans a portion of the screen; it must stay at the same y-axis throughout movement.

Here is a link to a game that has the exact same controls as I want: ‎The Line on the App Store

Below, is the code that I attached to the gameObject that I wanted to affect.

using UnityEngine;
using System.Collections;

public class Drag : MonoBehaviour {

    private Vector2 screenPoint;

    void OnMouseDrag()
    {
        Vector2 curScreenPoint = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
        Vector2 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint);
        transform.position = curPosition;
    }
}

This code enables me to drag my gameObject around anywhere on the screen, which is nice, but obviously not exactly what I want.

Thanks in advance.

You unnecessarily update position.y. The last line should look like this:

transform.position.x = curPosition.x;

Looks like the same question you posted here :confused:

caught red handed… CRIMINAL SCUM! PREPARE TO SUFFER!

jk

this is as complex as this should be:
transform,position.x = (code to get only this axis to be moving or whatever, simple lol, but NOOO, vector3, 4 5 6 7 7 8902019…) wasting my dam time, unity should be ashamed, and i gotta pay for this… seriously… i dont mind paying, but this is a pain in the ass.