- Home /
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.
Answer by VTW · Feb 04, 2014 at 04:35 PM
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.
Currently I simply change the world gravity at start to 0 in all directions, then on Left click it's set to -9.8 and on right click change it to 9.8 (or greater, currently in testing).
The game is sort of like what you said, it's a bungee jumping type of game where the player needs to stop at certain platforms to grab items, then they bounce back up and then dive back down. What I've got right now is just a rough prototype to test some basic mechanics like altering gravity. Being a bungee-esque thing the slowing to a stop before going back up is actually what I want.
The mouse tracking has yet to be resolved, the object still drops/moves toward the camera at high speeds even with gravity set to 0 and this is with the code robertbu provided (I've done a bit of mucking about with it but not much). Do you have any examples of working with lerp (example code or something like that)? The script reference's generic code is sort of useful but I learn better seeing something that has a defined purpose other than just showing syntax.
Personally I wouldn't change the gravity. It will have an effect on everything else you add to the game later on. It would be better if every alteration corresponding to the character were local, I$$anonymous$$O.
Where is your camera placed, and why is the character moving towards it and not the ground like a typical bungee?
I don't have any examples, as my familiarization with lerp was when I started with 3D. I've yet to implement in a 2D environment. There is, however, a great 5 $$anonymous$$ute tutorial on the main site. Though it covers 3D, it's essentially the same $$anonymous$$us one dimension :P
http://unity3d.com/learn/tutorials/modules/beginner/scripting/lerp
Hmm, I see your point on the gravity thing. I don't believe we'll have anything else that'll be affected by gravity but better to be safe than sorry. Any suggestions about how to do that? The gravity thing was just a test because I couldn't think of something else to use and that seemed the simplest option at the time.
As for the camera, I've got it set to the 2d option in Unity and I have it parented to the object. It is looking straight at the object. From the side it would look like this:
C O
(Okay apparently the formatting here doesn't allow large gaps between letters, just imagine there is a space between them, they are not right next to each other.)
C being the Camera and O being the object. And as for why it's moving toward the camera, I honestly couldn't tell you. When I don't have the mouse follow script in the code it simply falls straight down according to gravity, but when I put it in it drops rapidly as soon as I start the scene and moves in the direction the camera is/was (since the camera is parented to it the camera follows. Basically it moves in the -Z direction if I've got my axis right).
Also, thanks for the video. I'll give it a watch tomorrow, it's getting late.
Like I said above, I believe toggling the gravity on the rigidbody is possible programmatically.
Assu$$anonymous$$g your character is at the Z 0 position, I would simply assign the mousePos.z to 0. That way you know you're going to keep any movement to just the 2D coords X and Y.
Also, it should be moving in the direction of the mouse, not the camera if you're using robertbu's code. After manually setting the .z coord to 0, put the code in your falling object's GO. Without a lerp, it should instantly move to wherever the mouse is currently.
Ah, I actually tried that before. The problem with turning gravity on and off for a specific object is that, once it has be affected by gravity once, it keeps that even if you turn gravity off. It may not speed up if you turn gravity off but it will continue falling at the speed it had reached at the point you turned gravity off. Say the object had been falling for a second and reached a drop speed of -4.5 meters per second. If you turn off gravity it will still drop at -4.5 meters per second but it won't gain an more falling speed until you turn gravity back on.
Putting zero for mousePos.z in the code seems to have done the job of keeping it along the x axis, but it still seems to be dropping at a high rate (the Y position in the inspector drops by ~100 or more a second while the scale of the scene is set to meters). I get to work on adding a lerp and seeing if that fixes it though. Thanks for the help so far.
Answer by robertbu · Feb 04, 2014 at 04:55 AM
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613132457im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
C# move main camra via mouse position on boarders around screen script for RTS Game 0 Answers
How to rotate an object around another facing to mouse? 1 Answer
Unity Ui. Why constant pixel size option in Canvas Scaler don't work with mouse? 1 Answer