I’m trying to make a cannon ball get fired from a cannon, when I instantiate the cannon ball it doesn’t have any velocity even though I add it, and the ball just falls to the ground.
public GameObject projectile;
public float bulletSpeed;
void Update (){
if (Input.GetMouseButtonUp (0)) {
GameObject bullet = Instantiate (projectile, transform.position, transform.rotation) as GameObject;
bullet.GetComponent<Rigidbody> ().AddForce (transform.forward * 10);
}
}
Note that this script is attached to the cannon and the cannonball’s script doesn’t have anything to do with this.
@trbananer Regarding this line: bullet.GetComponent ().AddForce (transform.forward * 10);
I don’t know if its the new update, but some of the tutorials I saw suggested to define Rigidbody in your script, as direct references are no longer used? I don’t even know what that means, but try this:
rb = GetComponent<Rigidbody>();
rb.velocity = transform.forward * speed;
of course replacing speed with bulletSpeed.
Also, you define bulletSpeed, but don’t use it for anything that I can see, is that maybe being skipped?
Also, if you replace your transform.forward * 10 with * bulletSpeed, remember to give it a value in unity on the object.
Hope it helps!
I think the problems for you is gravity is greater than the force you assert that why the canon ball fall down.