Moving something to mouse position on the X-plane in C#.

I’m working on coding a 2d game and I need an object to follow the mouse position. I’ve currently got some code for it but the object will move into the scene or toward the camera depending on how I mess with variables instead of just staying at a set depth and it also moves at an incredible speed. I’m not sure how to fix these problems since I’m not sure how to code this in general (The code I have I got from another answer).

Vector3 mousePos = new Vector3 (Input.mousePosition.x, 0, 0);

myTransform.position = Camera.main.ScreenToWorldPoint(mousePos);

Nothing exceptionally complicated but I’m not really sure how to alter it to fix the problems.

Any help would be appreciated.

EDIT: You’ll have to excuse me screwing up the title, I was rather tired.

I should clarify some other things about my code. The object I’m controlling is falling and I need to move it to the left and right during it’s decent. But, I’ve also got a code setup that allows me to reverse the gravity upon a button press and then un-reverse it with another button press. The object is supposed to start stationary and, when I press the left mouse button, gravity should kick in. The problem with the current code (Including the one recently suggested) is that the object drops at a ridiculous speed (in addition to moving towards the camera but that could simply be caused by me switching from orthographic view to perspective view to get a better look at how the object drops) and reversing gravity has no effect, the object continues to fall. Either way, the object drops far to fast, and as I said, until I press the left mouse button gravity doesn’t take effect.

The gravity controls worked before I tried to add the mouse follow so it’s not that that stuff isn’t working.

Do you use a rigidbody for your object? If so, try unchecking the “use gravity” box since you want to implement gravity on your own. If you’d like, you could set the left click to simply enable gravity on said rigidbody, allowing it to fall (you mentioned enabling gravity with a left click).

I assume this is a game where a freefalling object moves left and right to collect various items or some such.

Since you seem to want to reverse gravity on an object, your other click could be to directly change the velocity of a rigidbody (not recommended), or to add and keep adding a force (rigidbody.addforce()) to the rigidbody so long as the button is held down. By default, gravity is set at -9.81 units. If you added a force directly opposite to gravity, it would slow the object to the point it just stops moving. To move in reverse would obviously require a greater force to be added. To move in reverse immediately would require adding a huge force and then backing off the force amount relative to the current velocity of the object so your object doesn’t start flying upwards at a ridiculous velocity (becomes a simple math problem).

As far as the mouse tracking goes…Not sure if you got that resolved or not, but you may want to look into lerp for smooth movement based on a speed you set. Be sure that you don’t call lerp over and over though (like if you put it in update) otherwise you’ll get some funky results.

For an orthographic camera, I’ll offer this suggestion:

Vector3 mousePos = new Vector3 (Input.mousePosition.x, 0, 0);
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
mousePos.z = transform.position.z;
transform.position = mousePos;

If you are using a perspective camera, this will have to change.