- Home /
Shoot Ball in Direction a Target
I'm with a problem I want to shoot the ball toward a target I have experience with scripting, I've tried several ways but the ball just did not shoot at the target can anyone tell me what I'm doing wrong?
Vector3 shoot = ball.transform.TransformDirection(target.position);
ball.rigidbody.AddForce ( shoot*4.0f);
Hi, $$anonymous$$y friend I am trying to do shooting but I can't doing it could you please help to share the codes. Thanks.
Answer by robertbu · Nov 16, 2014 at 07:02 AM
You are converting your target to a local position, but AddForce() takes a world position. A typical solution to this problem would be:
Vector3 shoot = (target.position - ball.transform.position).normalized;
ball.rigidbody.AddForce(shoot * 500.0f);
For a one-time application of force on an object with a mass of 1.0, you likely want values in the 250 - 2000 range depending on the situation and your goal.
thank you friend now worked in my case I'm shooting a soccer ball into the goal what would be best "Add Force" or "Velocity"?
Either would work for a one-time application of force. For repeated applications, AddForce() tends to produce more realistic result.
Your answer
Follow this Question
Related Questions
how to make a shooting target ? 2 Answers
Script for shooting a target 0 Answers
Moving Enemies? 1 Answer