C# Moving object on x axis with mouse move

Hi, I am trying to create a game where the player drops a cube from the top of the screen to the bottom, collecting points on the way down depending on where they dropped the cube from (like a Coin Pusher arcade game).

At the moment I am able to have my cube move to the mouse, but this in both X and Y values. See -
Camera.main.ScreenPointToRay(Input.mousePosition);
Vector3 mouse = Input.mousePosition;
mouse = mouse;
transform.position = Camera.main.ScreenToViewportPoint(mouse);

I am keen to have movements only on X but am unsure how to achieve this.

Thank you,

Kittik

Just use the result of ScreenToViewportPoint(), create a new Vector3 from it, but leave the Y at 0.

I have ‘fixed’ the issue by changing the first posts code to:
Vector3 mouse = Camera.main.ScreenToViewportPoint(Input.mousePosition);
transform.position = new Vector3(mouse.x, transform.position.y, transform.position.z);

But the movement from a mouse is very slow and not very responsive. Is there a way to speed up, or more accurately follow my mouse movements?

1 Like

Sounds to me like low framerate not an issue with this code specifically. What’s your framerate? (click on stats in the play window)

lol its the code