Won't fire when switching weapons?
So I am working on a game based off of the Space Shooter Tutorial game. Just implemented weapons floating in space that can be picked up and switched between using the 1, 2, and 3 keys. Currently trying to get it so I can only fire a weapon when the particular weapon has already been picked up and selected. I'm starting off with the first weapon, figured it was easier than trying to figure out all 3 at once. Here is my most recent player controller script.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[System.Serializable]
public class Boundary
{
public float xMin, xMax, zMin, zMax;
}
public class PlayerController : MonoBehaviour {
public float speed;
public Boundary boundary;
public float tilt;
public float secPerShot;
private float nextFire;
private int wepSel;
public WeaponBase wepA;
public WeaponBase wepB;
public WeaponBase wepC;
public GameObject shot;
public Transform shotSpawn;
private List<WeaponBase> weps;
public bool wepUp1;
public bool wepUp2;
public bool wepUp3;
void Awake ()
{
weps = new List<WeaponBase> ();
weps.Add (wepA);
weps.Add (wepB);
weps.Add (wepC);
foreach(WeaponBase w in weps)
w.gameObject.SetActive(false);
wepUp1 = false;
}
void Update ()
{
nextFire += Time.deltaTime;
if (Input.GetKeyDown ("1") && wepUp1) {
weps [0].gameObject.SetActive (true);
weps [1].gameObject.SetActive (false);
weps [2].gameObject.SetActive (false);
FireSelect();
}
if (Input.GetKeyDown ("2") && wepUp2) {
weps [0].gameObject.SetActive (false);
weps [1].gameObject.SetActive (true);
weps [2].gameObject.SetActive (false);
}
if (Input.GetKeyDown ("3") && wepUp3) {
weps [0].gameObject.SetActive (false);
weps [1].gameObject.SetActive (false);
weps [2].gameObject.SetActive (true);
}
}
void FixedUpdate () {
float hor = Input.GetAxis ("Horizontal");
float ver = Input.GetAxis ("Vertical");
Vector3 vel = new Vector3 (hor, 0, ver);
GetComponent<Rigidbody>().velocity = vel*speed;
GetComponent<Rigidbody>().position = new Vector3 (
Mathf.Clamp (GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp (GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
);
GetComponent<Rigidbody>().rotation = Quaternion.Euler (0, 0, GetComponent<Rigidbody>().velocity.x * -tilt);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Weapon1"))
{
other.gameObject.SetActive(false);
wepUp1 = true;
}
if (other.gameObject.CompareTag("Weapon2"))
{
other.gameObject.SetActive(false);
wepUp2 = true;
}
if (other.gameObject.CompareTag("Weapon3"))
{
other.gameObject.SetActive(false);
wepUp3 = true;
}
}
void FireSelect()
{
if (weps[0] == true && Input.GetButtonDown ("Jump"))
{
wepA.shoot();
}
}
}
The shoot method being called in the FireSelect method is in a different weapon script that instantiates the shots. Let me know if there's anything else you think could help make this easier to figure out. I feel like im thinking the wrong way about something here. I appreciate any help than you all can give me!
Your answer
Follow this Question
Related Questions
Unity Multiplayer - Projectile hits owner 0 Answers
Bullet shooting in the wrong angle. 0 Answers
My script does not work each time i enter Unity unless i add it to the object again. any solutions? 0 Answers
Easy question: X axis overriding Y. Please help! :D 0 Answers
My laser bolts are not working in space shooter. If someone could help, that would be great! 1 Answer