Skip to content

Instantly share code, notes, and snippets.

@IronMonk-UK
Created March 7, 2016 11:43
Show Gist options
  • Save IronMonk-UK/a3cb8977daf1af3b36f4 to your computer and use it in GitHub Desktop.
Save IronMonk-UK/a3cb8977daf1af3b36f4 to your computer and use it in GitHub Desktop.
The scripts relating to the modular spaceship project created.
using UnityEngine;
using System.Collections;
public class barScale : MonoBehaviour {
public stats shipStats;
public int maxStat;
public string type;
void Start () {
}
void Update () {
float xScale = 0;
// Resize bar based on stat percentage
switch (type) {
case "health":
xScale = ((float)shipStats.health / (float)maxStat);
break;
case "speed":
xScale = ((float)shipStats.speed / (float)maxStat);
break;
case "cap":
xScale = ((float)shipStats.cap / (float)maxStat);
break;
case "man":
xScale = ((float)shipStats.man / (float)maxStat);
break;
case "fireRate":
xScale = ((float)shipStats.fireRate / (float)maxStat);
break;
case "regen":
xScale = ((float)shipStats.regen / (float)maxStat);
break;
case "damage":
xScale = ((float)shipStats.damage / (float)maxStat);
break;
}
transform.localScale = new Vector3 (xScale ,1,1);
}
}
using UnityEngine;
using System.Collections;
public class bulletScript : MonoBehaviour {
public float speed;
public float timer;
private Rigidbody bulletBody;
void Start ()
{
bulletBody = GetComponent<Rigidbody> ();
}
void Update ()
{
// Keep the bullet moving
bulletBody.AddForce (transform.up * speed);
timer += Time.deltaTime;
// Destroy bullet after 3 seconds
if (timer >= 3.0f)
{
Destroy (gameObject);
}
}
}
using UnityEngine;
using System.Collections;
public class cockpitScript : MonoBehaviour {
public int maneuver;
public int fireRate;
public int health;
void Start () {
}
void Update () {
}
}
using UnityEngine;
using System.Collections;
public class gunScript : MonoBehaviour {
public GameObject bullet;
public Transform bulletSpawn;
public int fireRate;
public int damage;
private int fireWait;
private bool canFire = true;
void Update ()
{
// Keep counting fire rate delay
if (!canFire)
{
fireWait++;
}
if (fireWait >= fireRate)
{
canFire = true;
}
}
public void Fire ()
{
// Fire if the fire rate delay permits and mouse button is clicked to call this function
if (canFire) {
Instantiate (bullet, bulletSpawn.position, transform.rotation);
fireWait = 0;
canFire = false;
}
}
}
using UnityEngine;
using System.Collections;
public class hullScript : MonoBehaviour {
public int health;
public int speed;
public int shieldCap;
void Start ()
{
}
void Update ()
{
}
}
using UnityEngine;
using System.Collections;
public class menu : MonoBehaviour {
public GameObject hull;
public GameObject wings;
public GameObject engine;
public GameObject cockpit;
public GameObject gunLeft;
public GameObject gunRight;
public GameObject shield;
public GameObject[] hullParts;
public GameObject[] wingsParts;
public GameObject[] engineParts;
public GameObject[] cockpitParts;
public GameObject[] leftGunParts;
public GameObject[] rightGunParts;
public GameObject[] shieldParts;
private hullScript hullPart;
private wingsScript wingsPart;
private engineScript enginePart;
private cockpitScript cockpitPart;
private gunScript leftGunPart;
private gunScript rightGunPart;
private shieldScript shieldPart;
private int currentHull;
private int currentWings;
private int currentEngine;
private int currentCockpit;
private int currentLeftGun;
private int currentRightGun;
private int currentShield;
public GameObject instructions;
public GameObject camera;
void Start ()
{
// Keep ship settings to carry to game
DontDestroyOnLoad (transform.gameObject);
}
void Update ()
{
// Quit with escape as cursor is locked
if (Input.GetKeyDown(KeyCode.Escape))
{
Application.Quit();
UnityEditor.EditorApplication.isPlaying = false;
}
}
public void ChangeHull ()
{
currentHull++;
if ( currentHull >= hullParts.Length )
currentHull = 0;
Destroy(hull);
hull = (GameObject)Instantiate(hullParts[currentHull], transform.position, transform.rotation);
hull.transform.parent = transform;
hull.transform.Rotate(new Vector3(270, 0, 0));
hullPart = hull.GetComponent<hullScript>();
}
public void ChangeWings ()
{
currentWings++;
if ( currentWings >= wingsParts.Length )
currentWings = 0;
Destroy(wings);
wings = (GameObject)Instantiate(wingsParts[currentWings], transform.position, transform.rotation);
wings.transform.parent = transform;
wings.transform.Rotate(new Vector3(270, 0, 0));
wingsPart = wings.GetComponent<wingsScript>();
}
public void ChangeEngine ()
{
currentEngine++;
if ( currentEngine >= engineParts.Length )
currentEngine = 0;
Destroy(engine);
engine = (GameObject)Instantiate(engineParts[currentEngine], transform.position, transform.rotation);
engine.transform.parent = transform;
engine.transform.Rotate(new Vector3(270, 0, 0));
enginePart = engine.GetComponent<engineScript>();
}
public void ChangeCockpit ()
{
currentCockpit++;
if ( currentCockpit >= cockpitParts.Length )
currentCockpit = 0;
Destroy(cockpit);
cockpit = (GameObject)Instantiate(cockpitParts[currentCockpit], transform.position, transform.rotation);
cockpit.transform.parent = transform;
cockpit.transform.Rotate(new Vector3(270, 0, 0));
cockpitPart = cockpit.GetComponent<cockpitScript>();
}
public void ChangeLeftGun ()
{
currentLeftGun++;
if ( currentLeftGun >= leftGunParts.Length )
currentLeftGun = 0;
Destroy(gunLeft);
gunLeft = (GameObject)Instantiate(leftGunParts[currentLeftGun], transform.position, transform.rotation);
gunLeft.transform.parent = transform;
gunLeft.transform.Rotate(new Vector3(270, 0, 0));
leftGunPart = gunLeft.GetComponent<gunScript>();
}
public void ChangeRightGun ()
{
currentRightGun++;
if ( currentRightGun >= rightGunParts.Length )
currentRightGun = 0;
Destroy(gunRight);
gunRight = (GameObject)Instantiate(rightGunParts[currentRightGun], transform.position, transform.rotation);
gunRight.transform.parent = transform;
gunRight.transform.Rotate(new Vector3(270, 0, 0));
rightGunPart = gunRight.GetComponent<gunScript>();
}
public void ChangeShield ()
{
currentShield++;
if ( currentShield >= shieldParts.Length )
currentShield = 0;
Destroy(shield);
shield = (GameObject)Instantiate(shieldParts[currentShield], transform.position, transform.rotation);
shield.transform.parent = transform;
shield.transform.Rotate(new Vector3(270, 0, 0));
shieldPart = shield.GetComponent<shieldScript>();
}
public void LoadGame ()
{
Application.LoadLevel ("Prototype_Scene_01");
camera.SetActive (true);
gameObject.GetComponent<shipControl> ().enabled = true;
gameObject.GetComponent<shipControl> ().Astart ();
}
public void showInstructions ()
{
instructions.SetActive (true);
}
public void hideInstructions ()
{
instructions.SetActive (false);
}
}
using UnityEngine;
using System.Collections;
public class shieldScript : MonoBehaviour {
public int shieldCap;
public int shieldRegen;
void Start () {
}
// Update is called once per frame
void Update () {
}
}
using UnityEngine;
using System.Collections;
public class shipControl : MonoBehaviour {
public GameObject hull;
public GameObject wings;
public GameObject engine;
public GameObject cockpit;
public GameObject leftGun;
public GameObject rightGun;
public GameObject shield;
private hullScript hullPart;
private wingsScript wingsPart;
private engineScript enginePart;
private cockpitScript cockpitPart;
private gunScript leftGunPart;
private gunScript rightGunPart;
private shieldScript shieldPart;
private Rigidbody rigid;
public float thrustAmount;
public float torque;
public float turn;
public float minTurn;// = -0.5f;
public float maxTurn;// = 0.5f;
public float maxRotate;// = 50;
public float minRotate;// = -50;
public float maxThrust;// = 25;
public float rotate;
private int health;
private int speed;
private int man;
private int cap;
private int regen;
private int maxHealth;
public GameObject healthbar;
public stats _stats;
private KeyCode fire = KeyCode.Mouse0;
public void Astart ()
{
engine = gameObject.GetComponent<menu> ().engine;
hull = gameObject.GetComponent<menu> ().hull;
wings = gameObject.GetComponent<menu> ().wings;
leftGun = gameObject.GetComponent<menu> ().gunLeft;
rightGun = gameObject.GetComponent<menu> ().gunRight;
cockpit = gameObject.GetComponent<menu> ().cockpit;
shield = gameObject.GetComponent<menu> ().shield;
enginePart = engine.GetComponent<engineScript>();
hullPart = hull.GetComponent<hullScript>();
wingsPart = wings.GetComponent<wingsScript>();
leftGunPart = leftGun.GetComponent<gunScript>();
rightGunPart = rightGun.GetComponent<gunScript>();
cockpitPart = cockpit.GetComponent<cockpitScript>();
shieldPart = shield.GetComponent<shieldScript>();
health = _stats.health;
speed = _stats.speed;
man = _stats.man;
cap = _stats.cap;
regen = _stats.regen;
minTurn = man * -12;
maxTurn = man * 12;
minRotate = man * -12;
maxRotate = man * 12;
gameObject.GetComponent<stats> ().enabled = false;
maxHealth = health;
}
void Awake ()
{
rigid = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
rigid.AddTorque(transform.up * torque * turn);
rigid.AddForce(transform.forward * thrustAmount);
if (transform.rotation.x <= maxRotate && transform.rotation.x >= minRotate) {
rigid.AddTorque(transform.right * torque * rotate);
}
}
void Update ()
{
Screen.lockCursor = true;
// Use physics engine to turn the ship
if ( Input.GetAxis("Vertical") >= 1 )
{
thrustAmount = Mathf.Min(thrustAmount + 2.5f, maxThrust);
} else {
thrustAmount = Mathf.Max(0, thrustAmount - 5.0f);
}
turn = Input.GetAxis("Horizontal");
if (turn > maxTurn)
turn = maxTurn;
if (turn < minTurn)
turn = minTurn;
rotate = Input.GetAxis ("Rotate");
// Fire both guns when mouse is down
if (Input.GetKey(fire))
{
leftGunPart.Fire ();
rightGunPart.Fire ();
}
// Game over when no health left
if(health == 0)
{
// Destroy ship so it doesn't carry over
Destroy(gameObject);
// Load game over screen
Application.LoadLevel("Scene_02_GameOver");
}
}
void OnCollisionEnter(Collision collision)
{
// Reduce health on impact
health--;
// Update health bar
float hpPercent = ((float)health/(float)maxHealth)*1.64f;
Vector3 newScale = new Vector3(hpPercent, 0.48f, 1f);
healthbar.transform.localScale = newScale;
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class stats : MonoBehaviour {
public GameObject hull;
public GameObject wings;
public GameObject engine;
public GameObject cockpit;
public GameObject leftGun;
public GameObject rightGun;
public GameObject shield;
private hullScript hullPart;
private wingsScript wingsPart;
private engineScript enginePart;
private cockpitScript cockpitPart;
private gunScript leftGunPart;
private gunScript rightGunPart;
private shieldScript shieldPart;
public int health;
public int speed;
public int man;
public int cap;
public int regen;
public int fireRate;
public int damage;
public Text healthText;
public Text speedText;
public Text manText;
public Text capText;
public Text regenText;
public Text fireText;
public Text damText;
void Start () {
}
void Update ()
{
engine = gameObject.GetComponent<menu> ().engine;
hull = gameObject.GetComponent<menu> ().hull;
wings = gameObject.GetComponent<menu> ().wings;
leftGun = gameObject.GetComponent<menu> ().gunLeft;
rightGun = gameObject.GetComponent<menu> ().gunRight;
cockpit = gameObject.GetComponent<menu> ().cockpit;
shield = gameObject.GetComponent<menu> ().shield;
enginePart = engine.GetComponent<engineScript>();
hullPart = hull.GetComponent<hullScript>();
wingsPart = wings.GetComponent<wingsScript>();
leftGunPart = leftGun.GetComponent<gunScript>();
rightGunPart = rightGun.GetComponent<gunScript>();
cockpitPart = cockpit.GetComponent<cockpitScript>();
shieldPart = shield.GetComponent<shieldScript>();
// Calculate player stats
health = hullPart.health + enginePart.health + cockpitPart.health;
speed = hullPart.speed + wingsPart.speed + enginePart.speed;
man = wingsPart.maneuver + cockpitPart.maneuver;
cap = hullPart.shieldCap + shieldPart.shieldCap;
regen = enginePart.shieldRegen + shieldPart.shieldRegen;
fireRate = leftGunPart.fireRate + rightGunPart.fireRate;
damage = leftGunPart.damage + rightGunPart.damage;
// Update text display
healthText.text = health.ToString();
speedText.text = speed.ToString ();
manText.text = man.ToString ();
capText.text = cap.ToString ();
regenText.text = regen.ToString ();
fireText.text = fireRate.ToString ();
damText.text = damage.ToString ();
}
}
using UnityEngine;
using System.Collections;
public class turretDetection : MonoBehaviour {
public turretScript _turret;
void Start () {
}
void Update () {
}
void OnTriggerEnter(Collider target)
{
if (target.tag == "Player")
{
_turret.seen = true;
}
}
}
using UnityEngine;
using System.Collections;
public class turretScript : MonoBehaviour {
public GameObject _base;
public GameObject _swivel;
public GameObject _gun;
private GameObject _ship;
public GameObject _detection;
public bool seen;
private int health = 10;
void Start ()
{
_ship = GameObject.FindGameObjectWithTag ("Player");
}
void Update ()
{
if (health == 0)
{
Destroy(gameObject);
}
}
void OnCollisionEnter(Collision target)
{
if (target.gameObject.tag == "Bullet")
{
health--;
Debug.Log (health);
}
}
}
using UnityEngine;
using System.Collections;
public class wingsScript : MonoBehaviour {
public int maneuver;
public int speed;
void Start () {
}
void Update () {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment