From eb2d751adf773c33bb99943be3d8ba98a20176b7 Mon Sep 17 00:00:00 2001 From: Trianta <56975502+Trimutex@users.noreply.github.com> Date: Thu, 14 Mar 2024 19:23:52 -0500 Subject: [PATCH] Adding holding spacebar to fire --- Assets/Scenes/_Scene_0.unity | 1 + Assets/__Scripts/Hero.cs | 11 +++++ Assets/__Scripts/Hero.cs~ | 95 ++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 Assets/__Scripts/Hero.cs~ diff --git a/Assets/Scenes/_Scene_0.unity b/Assets/Scenes/_Scene_0.unity index 14cc971..c1e8f81 100644 --- a/Assets/Scenes/_Scene_0.unity +++ b/Assets/Scenes/_Scene_0.unity @@ -548,6 +548,7 @@ MonoBehaviour: gameRestartDelay: 2 projectilePrefab: {fileID: 390831642383508558, guid: d58eefa3468510199b6e895172b42b17, type: 3} projectileSpeed: 40 + firingDelay: 0.15 _shieldLevel: 1 --- !u!1 &1065119440 GameObject: diff --git a/Assets/__Scripts/Hero.cs b/Assets/__Scripts/Hero.cs index 498157c..9cdb500 100644 --- a/Assets/__Scripts/Hero.cs +++ b/Assets/__Scripts/Hero.cs @@ -14,11 +14,14 @@ public class Hero : MonoBehaviour public float gameRestartDelay = 2f; public GameObject projectilePrefab; public float projectileSpeed = 40; + public float firingDelay = 0.02f; [Header("Set Dynamically")] public float _shieldLevel = 1; // This variables holds a reference to the last triggering GameObject private GameObject lastTriggerGo = null; + private bool isFiring = false; + private float lastFiringTime = 0; void Awake() { if (S == null) { @@ -51,7 +54,15 @@ public class Hero : MonoBehaviour transform.rotation = Quaternion.Euler(yAxis*pitchMult,xAxis*rollMult,0); // Allow the ship to fire if (Input.GetKeyDown(KeyCode.Space)) { + isFiring = true; + } + lastFiringTime += Time.deltaTime; + if (isFiring && (lastFiringTime > firingDelay)) { TempFire(); + lastFiringTime = 0; + } + if (Input.GetKeyUp(KeyCode.Space)) { + isFiring = false; } } diff --git a/Assets/__Scripts/Hero.cs~ b/Assets/__Scripts/Hero.cs~ new file mode 100644 index 0000000..498157c --- /dev/null +++ b/Assets/__Scripts/Hero.cs~ @@ -0,0 +1,95 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Hero : MonoBehaviour +{ + static public Hero S; + + [Header("Set in Inspector")] + // These fields control the movement of the ship + public float speed = 30; + public float rollMult = -45; + public float pitchMult = 30; + public float gameRestartDelay = 2f; + public GameObject projectilePrefab; + public float projectileSpeed = 40; + + [Header("Set Dynamically")] + public float _shieldLevel = 1; + // This variables holds a reference to the last triggering GameObject + private GameObject lastTriggerGo = null; + + void Awake() { + if (S == null) { + S = this; + } else { + Debug.LogError("Hero.Awake() - Attempted to assign second Hero.S!"); + } + } + + // Start is called before the first frame update + void Start() + { + + } + + // Update is called once per frame + void Update() + { + // Pull in information from the Input class + float xAxis = Input.GetAxis("Horizontal"); + float yAxis = Input.GetAxis("Vertical"); + + // Change transform.position based on the axes + Vector3 pos = transform.position; + pos.x += xAxis * speed * Time.deltaTime; + pos.y += yAxis * speed * Time.deltaTime; + transform.position = pos; + + // Rotate the ship to make it feel more dynamic + transform.rotation = Quaternion.Euler(yAxis*pitchMult,xAxis*rollMult,0); + // Allow the ship to fire + if (Input.GetKeyDown(KeyCode.Space)) { + TempFire(); + } + } + + void TempFire() { + GameObject projGO = Instantiate(projectilePrefab); + projGO.transform.position = transform.position; + Rigidbody rigidB = projGO.GetComponent(); + rigidB.velocity = Vector3.up * projectileSpeed; + } + + void OnTriggerEnter(Collider other) { + Transform rootT = other.gameObject.transform.root; + GameObject go = rootT.gameObject; + //print("Triggered: " + go.name); + // Make sure it's not the same triggering go as last time + if (go == lastTriggerGo) { + return; + } + lastTriggerGo = go; + if (go.tag == "Enemy") { + shieldLevel--; + Destroy(go); + } else { + print("Triggered by non-Enemy: " + go.name); + } + } + public float shieldLevel { + get { + return(_shieldLevel); + } + set { + _shieldLevel = Mathf.Min(value, 4); + // If the shield is going to be set to less than zero + if (value < 0) { + Destroy(this.gameObject); + // Tell Main.S to restart the game after a delay + Main.S.DelayedRestart(gameRestartDelay); + } + } + } +}