- Home /
throw object at specific direction or angle
I am trying to throw a gameobject on swipe direction similar to paper toss game i tried Addforce but i cant throw at swipe direction please suggest any solution.
bottom line : i want to throw object at enemy by swiping. please help me through
if(Input.GetMouseButtonDown(0))
{
initPos = Input.mousePosition;
}
if(Input.GetMouseButtonUp(0))
{
finalPos = Input.mousePosition;
finalPos = Camera.main.ScreenToWorldPoint(finalPos);
rigidbodyBox = ball.AddComponent("Rigidbody") as Rigidbody;
rigidbodyBox.mass = 5;
ball.rigidbody.AddForce(???????)//what should i pass here //******
}
Answer by Graham-Dunnett · Jan 21, 2014 at 10:45 AM
Track the movement of the finger on the touch pad. This will allow you to work out the direction of the swipe. Convert that (2d) direction vector into a 3d direction vector and apply the force in that direction.
converting is main issue i get 2d then how do i convert it to 3d direction I used Camera.ScreenToWorldPoint not working throwing object at different direction
if(Input.Get$$anonymous$$ouseButtonDown(0))
{
initPos = Input.mousePosition;
}
if(Input.Get$$anonymous$$ouseButtonUp(0))
{
finalPos = Input.mousePosition;
finalPos = Camera.main.ScreenToWorldPoint(finalPos);
rigidbodyBox = ball.AddComponent("Rigidbody") as Rigidbody;
rigidbodyBox.mass = 5;
ball.rigidbody.AddForce(???????)//what should i pass here //******
}
Answer by Ramael · Jul 10, 2015 at 08:36 AM
Maybe something like this could work:
gameObject.GetComponent<ConstantForce>.force = <YOUR SWIPE>.transform.TransformDirection(Vector3.forward);
It works for my as I have a ball that I throw in my arrows direction (which I can control with Left and Right keys). For me it looks like this:
Ball.GetComponent<ConstantForce>.force = Arrow.transform.TransformDirection(Vector3.forward);
This will throw in the direction of the arrow.
Your answer
![](https://koobas.hobune.stream/wayback/20220613131020im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How can I convert velocity/direction to Force? 3 Answers
AddForce on Object at Direction of Click 2 Answers
Rigidbody.AddTorque/AddForce Question. 1 Answer
How do I add force in the on trigger 1 Answer
AddForce with direction tuning 0 Answers