- Home /
Add force for long pass ball
Hi How to add correct forces for ball pass. For simple pass I do it that way
rb.AddForce((target.position - transform.position) * thrust, ForceMode.Impulse);
so basicaly I calculate direction and apply some thurst (kick power). It behavior a little strange because it looks that summary force depends on how far the target.position is o_0. And second question when I want to make long pass I do it that
rb.AddForce(transform.up * 5, ForceMode.Impulse); // this should add vertical force to move ball up
rb.AddForce((target.position - transform.position) * thrust, ForceMode.Impulse);
but it don't work correctly because ball sometimes go to diferent direction (because I think of this rb.AddForce(transform.up * 5, ForceMode.Impulse);)
Answer by tormentoarmagedoom · May 10, 2019 at 11:09 AM
Hallo.
First Qust: AddForce is a vector. Doing this;
target.position - transform.position
You calculate a vector with the correct direction, but the magnitude of the vector depends on the distance between the 2 points. To have a better control of the force, you should convert that vector into a known magnitude vector with the same direction. Best way to do this is to Normalize the vector (so the magnitude will be always 1)
Second Qst:
Meybe the best solution if you want a parabole trajectory, is to add the vertical movement as a simple translate function (or changing directly its transform.position.y) To have a correct control of the height.
I think a good solution can be, once you know the origin and final points, you can calculate the middle point of that 2 points. Then while the object is moving increase its Y component a little each frame, until reach the midpoint (midpoint in X and Z components, as Y will change). Once reached this midpoint, decrease Y component again.
Good luck!
Bye!
Hi thanks for answer
For short pass I tried normalize vector and maybe if you also mention about that then it is right approach. I will try figure it out ;).
For long pass I did stupid mistake I used transform.up insted Vector3.up the correct code shold look like this rb.AddForce((target.position - transform.position) * thrust, Force$$anonymous$$ode.Impulse); rb.AddForce(Vector3.up *5, Force$$anonymous$$ode.Impulse);
And it is working ;)
Nice!
Using normalize, makes you know the "force quantity" independent of the distance. So its usefull to move things. You can also move it wothout forces to mantain a constant speed, using transform.translate or changing transform.position.
Good luck! Bye!
Your answer
Follow this Question
Related Questions
Rigidbody.AddRelativeForce after modifying gameObject's rotation 1 Answer
Should my ball gameobject have a rigidbody component? 0 Answers
Basic Jump Pad help 2 Answers
Can't use Rigidbody.AddForce to RaycastHit.point 1 Answer
Flight Control Issues 0 Answers