x


Throwing a ball, target unrecognized?

Hi,

i am not sure why my example does not work, i would like to throw a ball to a target, but the ball is thrown just behind the "launcher" (the cube) or goes in the air, but never goes toward my target. Would you know why? Here is an image (the target is on the left), so the ball seems to take a random value each time it is thrown :

alt text

and the code :

var target : Transform;
var cannonball : GameObject;

function Update () {
    if (Input.GetKeyDown("b")){  // press b to shoot
       var ball: GameObject = Instantiate(cannonball, transform.position, Quaternion.identity);
       var velocity = rigidbody.velocity;
       var targetVelocity = (target.position - transform.position).normalized;
         targetVelocity = targetVelocity * 25.0;
       var velocityChange = targetVelocity - velocity;
       //velocityChange.y = 0;
       //Vector3.forward*4, ForceMode.Impulse
       ball.rigidbody.AddForce(velocityChange, ForceMode.Force);
       Destroy(ball, 10);
    }
}

At the end i would like to throw the ball to a specific target and with an angle, like in this thread : http://answers.unity3d.com/questions/148399/shooting-a-cannonball.html

Thanks for your help

test.png (29.1 kB)
more ▼

asked Sep 09 '12 at 02:18 PM

Paul36 gravatar image

Paul36
9 3 10 11

(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1862
x1332
x173
x40

asked: Sep 09 '12 at 02:18 PM

Seen: 431 times

Last Updated: Sep 09 '12 at 02:18 PM