My projectile isn't getting fired with any velocity
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.
Answer by arnhmail · Oct 21, 2017 at 01:36 AM
@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 he used it correctly. Direct references mean you access rigidbody directly without go through the object component like this
private Rigidbody rb;
rb.Addforce = new Vector3 (x,y,z);
//cannot use this anymore you have to get component first
Answer by Fockducker11 · Oct 21, 2017 at 07:57 AM
I think the problems for you is gravity is greater than the force you assert that why the canon ball fall down.