- Home /
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 :
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
Your answer
Follow this Question
Related Questions
Android Build help needed regarding ball throw 1 Answer
how do i make this lag less? 1 Answer
Player-controlled rigidbody ball sometimes slowing down for no reason 0 Answers
Animation or smooth teleportation 0 Answers
Create an Enemy Animation 0 Answers