- Home /
Adding force to a golf ball
Hey, I was wondering if it were possible to add force to an object based on a users input. I have a golf game that has the user picking at what position to hit the ball to add the rotation and the force from a scale of 1 to 100 percent. Is there a way to add force based on these inputs so that when the club hits the ball, the ball will take off differently each timed based on different input? Any help will be appreciated.
Thanks
Answer by DarkMath · Nov 08, 2012 at 10:14 PM
I don't know if this helps but the tricky thing with Unity and PhysX is there is only an AddForce and not SetForce.
So to add lift to something like a golf ball you have to (I think) blank out the previous force before adding a new force. Otherwise every time step you will add the lift force and the ball will reach low earth orbit.
So what I did was keep track of the previous force and negate it then add the newly computed force:
if (previousLiftForce != Vector3.zero)
{
this.rigidbody.AddForce(new Vector3(-previousLiftForce.x, -previousLiftForce.y, -previousLiftForce.z));
}
currentLiftForce = new Vector3(0, LiftForce, 0);
this.rigidbody.AddForce(currentLiftForce);
previousLiftForce = currentLiftForce;
Answer by DarkMath · Nov 09, 2012 at 11:14 AM
Hi Stormizin,
FixedUpdate won't help, all that does is guarantee the same time step. It has nothing to do with how AddForce works.
I did find a post explaining kind of what I'm talking about:
I think I now know why there is no SetForce. Basically SetForce would only allow ONE FORCE to act on a rigid body. But many times you want more than one force acting on a rigid body. Like in the golf ball example you'd may want to add a Lift Force and also a Wind Force.
In the post above he talks about basically the same solution that I posted above, it's just written another way. Instead of two AddForce calls that I have he just subtracts the two vectors:
this.rigidbody.AddForce(previousLiftForce - currentLiftForce );
Your answer
Follow this Question
Related Questions
Sometimes the ball isn't moving 0 Answers
Show prediction for ball rolling on surface 1 Answer
How to correctly scale a mini golf game 2 Answers
Adding force to mini golf ball 0 Answers
How to calculate future movement like in golf games? 1 Answer