I am working on a player weapon switch script for my game and I am not sure what I am doing incorrectly. I can get the weapons to switch but only once and they do not switch back. Here is the code that I am working with.
var selectedWeapon : float = 0;
var projecile : Rigidbody;
static var projMuzzleVelocity : float = 60.0; // in metres per second
static var RateOfFire : float = 0.5;
static var Inaccuracy : float = 0.5;
static var fireTimer : float = 0.5;
static var hasShotGun : boolean = false;
static var hasHandGun : boolean = false;
print("Has HandGun:" + hasHandGun);
print("Has ShotGun:" + hasShotGun);
function Start()
{
fireTimer = Time.time + RateOfFire;
selectedWeapon = 0;
print("Selected Weapon"+selectedWeapon);
}
function Update ()
{
if(Input.GetKeyDown("1") )
{
selectedWeapon = 1;
print("Selected Weapon"+selectedWeapon);
}
else if(Input.GetKeyDown("2"))
{
selectedWeapon = 2;
print("Selected Weapon"+selectedWeapon);
}
if(Input.GetButtonUp("Fire1")&& hasHandGun == true && HealthController.AMMO != 0 && selectedWeapon == 1)
{
if (Time.time> fireTimer)
{
//GameObject.projectile;
var muzzleVelocityHG : Vector3 = transform.forward;
if (Inaccuracy != 0)
{
var rand1 : Vector2 = Random.insideUnitCircle;
muzzleVelocityHG += new Vector3(rand1.x, rand1.y, 0) * Inaccuracy;
}
muzzleVelocityHG = muzzleVelocityHG.normalized * projMuzzleVelocity;
//Debug.Log("Fire1");
clone = Instantiate(projecile, transform.position, transform.rotation);
clone.velocity = muzzleVelocityHG;
fireTimer = Time.time + RateOfFire;
HealthController.AMMO -= 1.0;
Destroy(clone.gameObject,1.5);
}
}
if( Input.GetButtonUp("Fire1")&& hasShotGun == true && HealthController.AMMO != 0 && selectedWeapon == 2)
{
if (Time.time> fireTimer)
{
//GameObject.projectile;
var muzzleVelocity : Vector3 = transform.forward;
if (Inaccuracy != 0)
{
var rand2 : Vector2 = Random.insideUnitCircle;
muzzleVelocity += new Vector3(rand2.x, rand2.y, 0) * Inaccuracy;
}
muzzleVelocity = muzzleVelocity.normalized * projMuzzleVelocity;
//Debug.Log("Fire1");
clone = Instantiate(projecile, transform.position, transform.rotation);
clone.velocity = muzzleVelocity;
fireTimer = Time.time + RateOfFire;
HealthController.AMMO -= 1.0;
Destroy(clone.gameObject,1.5);
}
}
}
I think it has something to do with me not storing my previous weapon but that is just my best guess. Any ideas on what I am doing wrong or what I could be doing better would be great.
Thanks for the help.