Simultaneous Keyboard Input From Different Keys

Hello all,

I have some experience with game programming. However, I am new to Unity and C#. I am currently working on a space invaders like game in order to get the basics down. I have created a spaceship that can move left, right and shoot. One of my issues is the space ship can’t move and shoot at the same time. This can get quite annoying especially since the space ship can shoot quite frequently. Any help would greatly appreciated. Here is the script that controls the space ship:

using UnityEngine;
using System.Collections;

public class SpaceShipController : MonoBehaviour {

	public GameObject bulletObject;

	public GameManager gameManager;

	public float shipSpeed;
	public float bulletSpeed = 1;

	private Vector3 spaceShip;

	private Quaternion spaceShipRotation;

	private Vector3 bulletPosition;

	private int coolDown = 10;

	private bool moveRight = false;
	private bool moveLeft = false;
	private bool fire = false;

	// Use this for initialization
	void Start () {
	
		spaceShip = transform.position;

		spaceShipRotation = transform.rotation;

		bulletObject.transform.position = bulletPosition;
	}
	
	// Update is called once per frame
	void Update () {

		coolDown--;

		inputHandler();

		this.transform.position = spaceShip;
	}

	void inputHandler() {

		if (Input.GetKeyDown(KeyCode.RightArrow)) {
			
			moveRight = true;
			
		}
		
		if (Input.GetKeyDown(KeyCode.LeftArrow)) {
			
			moveLeft = true;
			
		}
		
		if (Input.GetKeyUp(KeyCode.RightArrow)) {
			
			moveRight = false;
			
		}
		
		if (Input.GetKeyUp(KeyCode.LeftArrow)) {
			
			moveLeft = false;
			
		}
		
		if (Input.GetKeyDown(KeyCode.Space)) {
			
			fire = true;
		}
		
		if (Input.GetKeyUp(KeyCode.Space)) {
			
			fire = false;
		}
		
		if (moveRight == true) {
			
			spaceShip.x += shipSpeed;
			
		}
		
		if (moveLeft == true) {
			
			spaceShip.x -= shipSpeed;
			
		}
		

		if (coolDown <= 0) {

			if (fire == true) {
				
				Fire ();

				coolDown = 10;
			}

		}
	}

	void Fire () {

		for (var i = 0; i < 2; i++) {

			if (i == 0) {

				spaceShip = new Vector3 (transform.position.x + 0.9f, transform.position.y + 0.9f, transform.position.z);
			}

			else if (i == 1) {

				spaceShip = new Vector3 (transform.position.x - 0.9f, transform.position.y + 0.9f, transform.position.z);
			}

			Instantiate(bulletObject, spaceShip, spaceShipRotation);

			bulletObject.AddComponent<BulletBehaviour>();

			//gameManager.bullets[] = bulletObject;

			spaceShip = this.transform.position;
		}
	}
}

The only other code I have written is a script that make the bullets fly forward. Here it is:

using UnityEngine;
using System.Collections;

public class BulletBehaviour : MonoBehaviour {

	public SpaceShipController ship;

	private Vector3 shipPosition;

	// Use this for initialization
	void Start () {
	
		shipPosition = transform.position;
	}
	
	// Update is called once per frame
	void Update () {
	
		shipPosition.y += 1;

		transform.position = shipPosition;
	}
}

Besides this I only have one other problem. My SpaceShipController takes a gameObject as a variable so that I knows what to spawn when you shoot. However, as far as I can tell this approach requires me to have the gameObject in the hierarchy. Is there a way to point the space ship class to the bullet object without having it already in the game by default? Once again any help would be greatly appreciated.

try just check for the combination like:

if (Input.GetKeyDown(KeyCode.Space) && Input.GetKeyDown(KeyCode.LeftArrow))
{
  //move
  //shoot
}

and put your logic in the condition…