Adding holding spacebar to fire
This commit is contained in:
parent
cc09a66d1e
commit
eb2d751adf
@ -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:
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
95
Assets/__Scripts/Hero.cs~
Normal file
95
Assets/__Scripts/Hero.cs~
Normal file
@ -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<GameObject>(projectilePrefab);
|
||||
projGO.transform.position = transform.position;
|
||||
Rigidbody rigidB = projGO.GetComponent<Rigidbody>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user