How to make player (sphere) shoot the ball (another sphere)?

Hi, I’m having trouble with trying to be able to shoot the ball when the player collides with it. I tried adding force forward but the problem is that it shoots always in the same direction (which is forward). I want it to shoot at the direction depending on the spot where they collide. I’ve been thinking about using raycast but I don’t know how to do it as the player is constantly rotating and changing direction. What I put in the scene is two players, ball and the “stadium”. If there’s anyone who once solved this “problem” (I know it’s probably easy but I’ve been using Unity only for one week now, even though I’m not a beginner in programming) or knows how to solve it, I’ll be really grateful. If you need any more information or screenshots of a game, just let me know. Thanks in advance.

Given that both the player and the ball are spheres, you can do something like this:

function OnCollisionEnter(col : Collision) {
    var dir = (col.transform.position - transform.position).normalized;
    col.rigidbody.AddForce(dir * someForce);
}

‘someForce’ is a variable you define, and I’d start with a value of at least 500 if you left the mass of each of the spheres at 1.0.

Note if you plan in the future to replace the player sphere with another mesh, then I would suggest you look to the normal in the contact points array in the collision to get a direction, possibly in combination with the incoming velocity of the ball and Vector3.Reflect.

You can add the force based on the contact.point normal, at the contact.point.
Here’s the contact points reference page. Unity - Scripting API: Collision.contacts

This might come in handy too.

Not to be discouraging, but contact.points aren’t really first week of Unity lesson. Its not hard, just not something to start off with. xD

The best way to learn is by doing tutorials. Unity’s official tutorials are good place to start. And also the Unitycookie beginner series, and lunar lander series. :slight_smile:

One thing I wanna add tho is, try sticking with C# when programming. Most code you find online and on the asset store will be C#.