I am just beginning using Unity and I made a very simple gun (if you can call it that), which fires spheres. Here is my code for firing the gun:
var shootForce:float;
var bulletPrefab:Transform;
var bulletSpawner:Transform;
function Update () {
if (Input.GetKeyDown(KeyCode.Mouse0)) {
instanceBullet = Instantiate(bulletPrefab, bulletSpawner.position, Quaternion.identity);
instanceBullet.rigidbody.AddForce(transform.forward * shootForce);
}
}
Unfortunately the gun only correctly fires when moving sideways or backwards. Otherwise the bullets just fall to the ground. I have a feeling that this might have to do with the bullets colliding with one another, but I don’t know how to fix the problem. I have tried to move the bullet spawner around and change the shoot force, but neither worked.
Tried it, but it seems to be doing the exact same thing.
– cabellicar123WHATEVER HAPPENED TO ANSWERING A QUESTION!? Stop commenting when yo have an answer, please
– DaveAWHATEVER HAPPENED TO ANSWERING A QUESTION!? Stop commenting when yo have an answer, please
– DaveATried your second suggestion. Still no change.
– cabellicar123Was the Debug.Ray() pointing in the correct direction? Was OnCollisionEnter() reporting any hits and if so what was it hitting?
– robertbu