using UnityEngine; public class ShipControls : MonoBehaviour { public float speed = 10f; GameManager gameManager; public GameObject laserPrefab; public AudioSource fire; private bool superSpeed = false; public float xLimit = 7; public float yLimit = 7; public float reloadTime = 0.5f; float elapsedTime = 0; void Start() { gameManager = GameObject.FindObjectOfType(); } void Update() { // Keeping track of time for bullet firing elapsedTime += Time.deltaTime; // Move the player left and right float xInput = Input.GetAxis("Horizontal"); transform.Translate(xInput * speed * Time.deltaTime, 0f, 0f); float yInput = Input.GetAxis("Vertical"); transform.Translate(0f, yInput * speed * Time.deltaTime, 0f); //clamp the ship's x position Vector3 position = transform.position; position.x = Mathf.Clamp(position.x, -xLimit, xLimit); position.y = Mathf.Clamp(position.y, -yLimit, yLimit); position.z = Mathf.Clamp(position.y, 0f, 0f); transform.position = position; if (Input.GetKey(KeyCode.LeftShift)) { superSpeed = true; } else { superSpeed = false; } if (superSpeed == true) { speed = 15f; } else { speed = 10; } // Spacebar fires. The default InputManager settings call this “Jump” // Only happens if enough time has elapsed since last firing. if (Input.GetButtonDown("Jump") && elapsedTime > reloadTime ) { DoubleFire(); } } void DoubleFire() { //Instantiate the Bullet 1.2 units in front of the player Vector3 spawnPos1 = transform.position; Vector3 spawnPos2 = transform.position; spawnPos1 += new Vector3(-.25f, 0f, .5f); spawnPos2 += new Vector3(.25f, 0f, .5f); Instantiate(laserPrefab, spawnPos1, Quaternion.identity); Instantiate(laserPrefab, spawnPos2, transform.rotation); fire.Play(); elapsedTime = 0f; // Reset bullet firing timer } // If a meteor hits the player void OnTriggerEnter(Collider other) { Destroy(other.gameObject); if (other.gameObject.tag == "Meteor") { gameManager.SlowWorldDown(); gameManager.AdjustTime(-2f); } else { gameManager.AdjustTime(2f); } } }