How to implement

How to implement a shot like in angry birds? Without the slingshot, when you click on the object?

Use Rigidbody2D class to implement physics and use the AddForce method to implement the shot.

Thanks.

when I use
rb2D.AddForce(
Camera.main.ScreenToWorldPoint(Input.mousePosition)
); the object flies constantly forward and slightly up and not where to be the cursor.

The Rigidbody2D AddForce method applies force toward the direction, not on the position, you should provide the direction.

For example if you pass Vector2.right * 100 to AddForce method, then the object will move right.

And how to translate the coordinates of the mouse in the direction?

The direction is the normalized difference of object and mouse position.

Here is the pseudocode:

((object position) - (mouse position)).normlized

And then apply your force by:

((object position) - (mouse position)).normlized * myForce

Then pass the result to add force:

float myForce = 100f;
Vector2 force = ((object position) - (mouse position)).normlized * myForce;
rigidbody.AddForce (force);

And all done.

Hope this helps.

1 Like

Thanks, it helped! And how to see the flight trajectory?

There are plenty of resources that teach you how to implement the trajectory:

Hope this helps.

Thanks.

1 Like