Adding force in the opposite direction of a sphere on a 2d plane in c#?

I probably screwed that title up real bad.

Here is what I would like to know how to do in c#

I have a ball…sphere, or a circle (since its 2d, or i guess it might not matter, anyway). If a player clicks/taps near the ball, it pushes the ball away from the click position. The closer the click is to the ball, the more force is used to push the ball.

Here is an illustration of the ball and force points. http://i.imgur.com/DzWHPBe.png

If you press or click the top+left, the ball should move down+right. If you click close to the ball, it pushes it hard. If you click far from it, it barely pushes it.

Can someone explain what I would need to do to get this to work? Please be specific as possible because I am brand new to Unity and still learning the basics. Ideally, if you could explain the functions I would need and where, I will gladly look them up and see if I can figure out how to get them working. I really want to learn so I can apply my knowledge to other parts of my games when needed.

Thanks!

1527677--87996--$DzWHPBe.png

Sounds like you need to get a vector from the mouse to the ball, interpolate the force based on distance, then apply the force. This should put you on the right track:

// Getting a vector from A to B is given by (B - A)
Vector3 direction = ball.transform.position - cursorPosition;

// Find how far away we clicked as a percentage of the max distance clicking is allowed
float percentOfMaxRadius = direction.magnitude / maximumClickDistance;

// Clamp that value between zero and one, just to be safe, or use an if-statement to check whether we should proceed
percentOfMaxRadius = Mathf.Clamp01( percentOfMaxRadius );

// The force is determined by multiplying the normalized direction with a scalar we get based on our click distance
Vector3 force = direction.normalized * Mathf.Lerp(maxForce, minForce, percentOfMaxRadius);

// Then add the force to the rigidbody, or do your own calculations with the resultant force vector
ball.rigidbody.AddForce( force, ForceMode.Impulse );

Here’s a source for more info on finding the cursor’s position in 3D space, if you need it:
http://forum.unity3d.com/threads/73762-Cursor-position-on-3D-world

Best of luck! Did this answer your questions?

I will try this as soon as I get home from work. I appreciate your help with this!