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

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;

Maybe this help you.

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:

http://forum.unity3d.com/threads/34689-couple-of-questions-regarding-Physics?p=226267&viewfull=1#post226267

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 );