I am trying to create a method that or series of methods that allows a ship to shoot with out any key being pressed and will change depending on what the current weapon is. I am thinking a would need a recursive method but I just can’t wrap my head around how to get it to work. Here is my whole Ship code. Currently the space bar can be pressed to fire and 1-3 for weapon select. AutoFire1() does work because it is called from Start() but I cant get how to get it to change weapons. Anyone have any ideas? Any insight would be great. Thanks!
using UnityEngine;
using System.Collections;
public class ShipController : MonoBehaviour {
public float moveRate = 10f;
public GameObject projectile;
float xMin;
float xMax;
float yMin;
float yMax;
public float projectilespeed;
public float firingRate;
public float playerHealth;
public int currentWeapon = 1;
private LevelManager levelManager;
public AudioClip laserShot;
public GameObject thruster;
//public bool gameStarted = false;
// Use this for initialization
void Start () {
float distance = transform.position.z - Camera.main.transform.position.z;
Vector3 bottomleftmost = Camera.main.ViewportToWorldPoint(new Vector3(.04f,.04f,distance));
Vector3 toprightmost = Camera.main.ViewportToWorldPoint(new Vector3(.96f,.3f,distance));
Vector3 startpos = Camera.main.ViewportToWorldPoint(new Vector3(.5f,.1f,distance));
xMin = bottomleftmost.x;
yMin = bottomleftmost.y;
xMax = toprightmost.x;
yMax = toprightmost.y;
this.transform.position = startpos;
levelManager = GameObject.FindObjectOfType<LevelManager>();
Vector3 thrusterStartPosition = transform.position + new Vector3 (0, -0.45f, 0);
GameObject playerShipThruster = Instantiate(thruster, thrusterStartPosition, Quaternion.identity) as GameObject;
AutoFire();
}
// Update is called once per frame
void Update () {
//if(Input.GetKeyDown(KeyCode.Space)){
// gameStarted = true;
//}
if(Input.GetKey(KeyCode.LeftArrow)) {
transform.position += Vector3.left * moveRate * Time.deltaTime;
}
else if (Input.GetKey(KeyCode.RightArrow)) {
transform.position += Vector3.right * moveRate * Time.deltaTime;
}
if (Input.GetKey(KeyCode.UpArrow)) {
transform.position += Vector3.up * moveRate * Time.deltaTime;
}
else if (Input.GetKey(KeyCode.DownArrow)) {
transform.position += Vector3.down * moveRate * Time.deltaTime;
}
float newX = Mathf.Clamp(transform.position.x, xMin, xMax);
float newY = Mathf.Clamp(transform.position.y, yMin, yMax);
transform.position = new Vector3 (newX, newY, transform.position.z);
if(Input.GetKeyDown(KeyCode.Alpha1)){
currentWeapon = 1;
}
if(Input.GetKeyDown(KeyCode.Alpha2)){
currentWeapon = 2;
}
if(Input.GetKeyDown(KeyCode.Alpha3)){
currentWeapon = 3;
}
if(Input.GetKeyDown(KeyCode.Space)){
//if(gameStarted){
if(currentWeapon == 1){
InvokeRepeating("FireLaser1", 0.00001f, firingRate);
}
if(currentWeapon == 2){
InvokeRepeating("FireLaser1", 0.00001f, (firingRate * .5f));
}
if(currentWeapon == 3){
InvokeRepeating("FireLaser2", 0.00001f, (firingRate));
}
}
if(Input.GetKeyUp(KeyCode.Space)){
if(currentWeapon == 1){
CancelInvoke("FireLaser1");
}
if(currentWeapon == 2){
CancelInvoke("FireLaser1");
}
if(currentWeapon == 3){
CancelInvoke("FireLaser2");
}
}
//if (playerHealth <= 0){
// levelManager.GameLost();
//}
}
void OnTriggerEnter2D(Collider2D collider){
Projectile laser = collider.gameObject.GetComponent<Projectile>();
if (laser){
playerHealth -= laser.damageCaused();
laser.Hit();
if (playerHealth <=0){
levelManager.GameLost();
//Destroy(gameObject);
}
Debug.Log ("Hit by a Laser");
Destroy(laser);
}
}
void FireLaser1(){
Vector3 startPosition = transform.position + new Vector3 (0, 0.6f, 0);
GameObject laser = Instantiate(projectile, startPosition, Quaternion.identity) as GameObject;
laser.GetComponent<Rigidbody2D>().velocity = new Vector3(0,projectilespeed,0);
AudioSource.PlayClipAtPoint(laserShot, transform.position);
}
void FireLaser2(){
Vector3 startPosition1 = transform.position + new Vector3 (-0.3f, 0.6f, 0);
GameObject laser1 = Instantiate(projectile, startPosition1, Quaternion.identity) as GameObject;
laser1.GetComponent<Rigidbody2D>().velocity = new Vector3(0,projectilespeed,0);
AudioSource.PlayClipAtPoint(laserShot, transform.position);
Vector3 startPosition2 = transform.position + new Vector3 (0.3f, 0.6f, 0);
GameObject laser2 = Instantiate(projectile, startPosition2, Quaternion.identity) as GameObject;
laser2.GetComponent<Rigidbody2D>().velocity = new Vector3(0,projectilespeed,0);
AudioSource.PlayClipAtPoint(laserShot, transform.position);
}
void AutoFire1(){
if(currentWeapon == 1){
InvokeRepeating("FireLaser1", 0.00001f, firingRate);
}
}
}