making a physics game, how do i make a script that lets me drag whatever i have my mouse on and am clicking at the same time, and how do i make it so that when i let go with some motion, it sends it far like how it would in real life.
One approach.
-
When you click, if you clicked on an object, remember the object as the thing you’re dragging. It needs a Rigidbody2D with mass, and while you’re dragging, set the isKinematic false so it will be able to experience forces.
-
When you move the mouse, if you have a thing you’re dragging, figure out a position of the mouse and add forces to make the object move in the direction of that position. If you use forces, the Rigidbody2D will have velocity and more importantly, momentum.
-
When you release the mouse, forget the thing you’re dragging. The Rigidbody2D will use the last velocity and momentum it had to continue moving.
There are other approaches.
thanks
Take a look at my PhysicsExamples2D GitHub repo. It has many examples for the physics components etc.
Here’s one that shows how to perform physically-based dragging using the TargetJoint2D.