Bullets spawn multiple copies

I am working on a game which has two turrets. They are controlled individually for aiming purposes, however right now I have them both firing when I press the space bar. I am really confused about two things: First when the bullets are spawned the speed never changes, no matter what value I enter for bulletSpeed. Second and more important every time I hit the space bar multiple bullets spawn instead of just one for each turret. Sometimes the first time space is pressed just two bullets are spawned, but after that it is anywhere from 4 to 10 bullets that end up being spawned. For the life of me I cannot figure this out.

using UnityEngine;
using System.Collections;

public class TurretScript : MonoBehaviour {

public Rigidbody2D Bullet;
public float bulletSpeed = .01f;
private float turretRotationSpeed = 5f;
void Update () {
	if(Input.GetKey(KeyCode.S) && gameObject.tag == "T1")
	if(Input.GetKey(KeyCode.J) && gameObject.tag == "T2")
	if (Input.GetKey (KeyCode.F) && gameObject.tag == "T1")
		clockwise ();
	if (Input.GetKey (KeyCode.L) && gameObject.tag == "T2")
		clockwise ();
	if (Input.GetKey (KeyCode.Space)) {
					fire ();
		Debug.Log ("fire pressed");
    Rigidbody2D newBullet = (Rigidbody2D) Instantiate(Bullet, transform.position,   transform.rotation);
newBullet.velocity = transform.forward * bulletSpeed;

Input.GetKey() returns true whenever the key in question is being held down. Since Update method is run every frame, many many times per second, it’s very hard to press the key short enough time for just 1 bullet to be created.

What you probably want is to use Input.GetKeyDown() which returns true only on the Update call when the key was first pressed down.

API docs:



As for the velocity, it is said it shouldn’t be adjusted directly though i think what you are doing should work :/. Try using AddForce instead. Also check the rigidbody settings on the bullet