throw 2d object

Okay so i have this box in 2d with an hinge joint2d on it that i use to move the object around with and gives the apparence of dangling from where the mouse cursor clicked.

And when i try to throw it in any direction it cant, how can i make it “throw-able”?

see attached gif.

alt text

Do you want to throw this box programmatically?

When you release the object you need to apply a force to it. Right now the only force is gravity (assuming you are performing a translation while the mouse is clicked), so it just falls straight down. If you're performing a translation, you'll have to calculate what the force should be. An example would be capturing the last n number of mouse positions, and on release iterating through those positions to calculate force and direction based on the path the mouse took prior to release. EDIT: Took way too long to write that, what @jpthek9 said :)

1 Answer

1

You can set the velocity of the object based on the last position.

Vector2 LastPos;
void FixedUpdate()
{
rigidbody2D.velocity = (rigidbody2D.position - LastPos) / Time.deltaTime;
LastPos = rigidbody2D.position;
}

Aaaah thank you it worked perfectly =) Didnt think it would be that easy, but thanks!