Hey guys.
I want it to shoot straight out infront, yet it is shooting almost randomly in a weird position or even moving the bullet at all… (very strange)
My code:
public Rigidbody bulletObject;
public Transform bulletSpawnPoint;
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0)) {
Rigidbody fireBullet = Instantiate(bulletObject, bulletSpawnPoint.position, transform.rotation) as Rigidbody;
fireBullet.AddForce(Vector3.forward * 300);
}
}
No errors, just not shooting properly. Can someone help me please?
If your bullets have colliders, there’s a chance that the bullets may be colliding with each other before they can get very far. As long as the mouse button is held down, you’re spawning a physics rigidbody every single frame.
Depending on your game style, a more traditional FPS method would be to use a raycast to simulate the bullet instead of actually creating a bullet object.
AddForce uses the global coordinate system, so your bullets will always fire along the z-axis, regardless of which way your gun is facing. To make them fly in the direction they’re pointing use fireBullet.transform.forward * 300.