Instantiate not shooting in right direction

I am trying to make a gun shoot bullets, and I have this code:

var bulletcreate = Instantiate(bulletprefab, GameObject.Find("Cylinder").transform.position, Quaternion.identity);

bulletcreate.rigidbody.AddForce(transform.forward
  • bulletspeedm4);

I have the bullet shooting out of the right spot, but when I shoot, it shoots out to the left. I want it to shoot out straight, right in the center of the screen. also, the cylinder in the code is the point where the bullet spawns from, then launches forward.

When you use `Quaternion.identity` you are making the object align with the world, to make it align with the GameObject that this script is on use `transform.rotation` instead.

Things that could mess you up are

  • your character/camera is not facing forward (positive Z axis)

    If your character is built facing in the negative X direction then

  • your script is on a game object that is not rotating with the camera (`transform` is the Transform of the GameObject that the script is on)

    to use the transform of the camera add a var mainCamera : Transform; property to your script and link it to the camera in the inspector.