- Home /
Player weapon swap
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.
Answer by jayhatmaker · May 17, 2013 at 04:43 PM
Just to clarify, a few thing.
you start with weapon No. 0, and hit 1 to use weapon No.1, hit 2 to use weapon No.2 right? Are you asking us why it wont go back to weapon No.0?
If so, you need to edit the values of selectedWeapon when you hit the numeric keys 1&2.
also can you post your debug messages when you...
have weapon 1 selected and switch to weapon 2
switch back to weapon 1
No, I am asking why it will not go back between weapons 1 and 2. So I see it working like this, the player is walking around without a gun. They run over the handgun to pick it up and now have a weapon in slot 1. Then they find the shotgun and pick it up and now they have a gun in slot 2. The problem is that I once I get a weapon in slot 2 the weapon in slot 1 will never come back and I can not select slot 1 anymore, almost like the script is stuck on 2.
what happens when you hit "1" when your character has a shotgun? does it print the debug message like "Selected Weapon1"??
If it doesn't, you are not picking up your key input.
So after some more and more testing, its something to do with the way that I am changing weapons. Its like I am losing what was "$$anonymous$$apped" to slot 1 when I get something for slot 2.
If that's the case, you should take a closer look at your "weapon pickup" part of the code. maybe you are replacing the weapon ins$$anonymous$$d of adding it.
That is what I think I might be doing but I am not sure how I change that. Here is the code for one of my weapon pick ups.
function OnTriggerEnter( other : Collider )
{
if(other.tag == "Player")
{
ShootingController.hasHandGun = true;
ShootingController.RateOfFire = .1;
ShootingController.proj$$anonymous$$uzzleVelocity = 45;
ShootingController.Inaccuracy = 0.1;
// ShootingController.weaponNumber1 = true;
HealthController.A$$anonymous$$$$anonymous$$O += 100;
Destroy(gameObject);
}
}
Answer by Samsterdamn · May 17, 2013 at 05:09 PM
So all of the debug comments are working AFAIK. The weird thing is that if you pick up the shotgun, slot 2, and select slot 1, the handgun, and you do not have the handgun, you will not fire and then be able to switch back over to slot 2, the shotgun, and fire.
The problem of not being able to switch only comes after you fill up both slots with weapons.
This code sets the values on ShootingController, HealthController and destroys the object (guessing one that picked up) right?
ShootingController.hasHandGun = true;
... and this code enables the player to use the handgun I assume.
Since you are using boolean value to check the weapon in players inventory, try tracking the boolean value of ShootingController.hasHandGun in the first code.
you can also change the input code from
if(Input.Get$$anonymous$$eyDown("1") ){}
to
if(Input.Get$$anonymous$$eyDown("1") && ShootingController.hasHandGun ){}
so your key input will only be triggered if you actually have the weapon.
The && ShootingController.hasHandGun
did not work out. It just made the character shoot without any regard to any settings. But this got me thinking that I could possibly not be storing what I have in slot 1 and that is the reason that it does not come back when I get something for slot2. The problem is I am not sure how to save the data from slot one as I already thought I was doing that.
I was thinking that maybe there could be a way for me to store the data of the individual weapon pickup scripts in the Shooting Controller script after you picked up a weapon but I am not sure how to even go about doing that or if that is even the right way to go about that.
Your checking sounds all tangled up, so ins$$anonymous$$d of mending the code, it will be faster and cleaner to take time to re-think how each action should be laid out.
You could use an array to store whatever you pick up. This is useful if you are going to add more and more weapons. So, your player can have something like
here's a psudo code:
//let's say your character can only carry certain number of weapons
int[] playerWeapons;
int maxNumberOfWeapons = 5;
***in the initialize...something like***
int[] playerWeapons = new int[maxNumberOfWeapons];
***in your pick-up code****
bool weaponIsPickedUp = false;
for(int i=0;i<maxNumberOfWeapons;++i)
{
if (!weaponIsPickedUp)
{
if(playerWeapons[i]==0)
{
//let's say 1 is pistol. you should have a static ins$$anonymous$$d of hard coded number...
playerWeapons[i]=1;
weaponIsPickedUp=true;
}
}
}
//successfully picked up a weapon
if (weaponIsPickedUp)
{
destroy(whatever);
}
your shooting code also needs readjusting. you need to check the playerWeapons array, and selectedWeapon value should be the index of the array.