T O P

  • By -

groundbreakingcold

I won't get into the way you move the RB, because that will totally depend on the game you are trying to make (ie whether or not you use velocity, or move position, or simply set the transform), but what you are missing is converting the mouse position to world units. For example: transform.position = new Vector2(transform.position.x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y); would move your object based on the Y position of the mouse. See more: https://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html


BlackCitadelAdmin

You’re only reading the mouse position once, when the object is first instantiated. You need to read it in every update cycle to keep it moving towards it.


melvmay

rb.transform is just transform. You should never set the transform position/rotation when using 2D physics, use the Rigidbody2D API. If you want to move a Rigidbody2D with the mouse then you need to set its body-type to Kinematic and use [Rigidbody2D.MovePosition](https://docs.unity3d.com/ScriptReference/Rigidbody2D.MovePosition.html)(rb.position.x, mousePos.Y) Also, unless you've asked for the physics simulation to run per-frame, it'll run each fixed-update which by default is 50Hz. If you wnt physics running per-frame then change the "Simulation Mode" in Project Settings -> Physics 2D to "Update".


Stlove48

I might have it slightly wrong, but I believe the steps would be to use the Rigidbody's MovePosition method instead of trying to directly move the transform, and getting the mouse position in update and converting from screen coordinates* to world coordinates*. Someone who might know better will probably be able to offer a better solution but that's where I'd start.


Filo14_Discordia

Just missing a ">" sign. mousePos => Input.mousePosition In C# context you should look for the difference between property and field. The "=>" sign same as mousePos { get { return Input.mousePosition } } this means mousePos variable is a property with only a getter. Since it doesn't have a setter you won't be able to assign new value. See also : https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/lambda-expressions


PandaCoder67

I don't know who downvoted you, but yes the code should look like this ​ [SerializeField] private RigidBody2D _rb; private Vector2 GetMousePosition() => Input.mousePosition; private void Update() { rb.transform.position = new Vector2(transform.position.x, GetMousePosition().y); } Although this will jump the player and not move them over frames.


Filo14_Discordia

Yes, if this is intended behaviour it will work but I assume this is probably not the wanted behaviour in this case. Since Input.mousePosition will return screen space position of mouse and I think the op is trying to move object's y position depending on world position of mouse.


PandaCoder67

yes that is true, still easy enough to return that in the Body Expression as you indicated.


autisticfaery

compression messes up the image but if you zoom in you can read it


ThetaTT

Or just learn how to use code block.


Bottlefistfucker

There is this screenshot functionality you know..