Inaccurate shooting script for gun

Hello guys. I was making my own script to let my gun shoot.

I’ve tried a lot of ways, from raycasting to instantiating my bullet game object, and the last option is working near to perfectly.

The problem is, the script is inaccurate, becouse when my bullet spawn and goes foward, it goes under my crosshair.

Is there a wait to make it ‘right’? I mean, make my gun shoots like in other fps games?

Here are the scripts : Ppauser - Pastebin.com and Shooting - Pastebin.com
Here are screenshots : http://i67.tinypic.com/a9wewp.jpg and http://i64.tinypic.com/334uuxw.png and the last one http://i64.tinypic.com/zlqjj5.jpg

Maybe that will help you better understand my problem :slight_smile:

The bullet are likely inaccurate because you are creating them to the side of the camera but not adjusting the bullets to make sure they hit where the camera is looking. You can use a raycast to get the point the camera is looking at and the adjust the bullet so it travels towards that point.

RaycastHit hit; // the output of the raycast
            
//check to see if the camera is looking at anything, if it is get the exact point
if(Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit)) {
    //rotates the bullet so it is pointing at the exact point the player is looking at
    shooted.transform.LookAt(hit.point);
}

The bullet will still end up falling below the crosshair because of gravity but this, at least makes, the bullet initially accurate. You could also disable gravity for the bullets if you don’t want them to fall as they travel.

This wasn’t asked in the question but two other things that might cause problems in the future:

You are using Time.deltaTime instead of Time.fixedDeltaTime inside FixedUpdate so the cool down on firing might be much longer then you expect.

Instead of using AddForce, you might want to set the velocity of the bullet directly so you can know how fast the bullet moves.