- Home /
What's wrong with my weapon switching/swapping script?
I have looked at the FPS tutorial on Unity's website, but forgot their weapon switching script, and I wanted to attempt creating my own from scratch, but I can't seem to get it to work. I feel like it should work correctly. I haven't tested the weapon slots yet, but I was mostly just trying to get a weapon switching, like swapping current gun for the one on the ground, thing working first:
private var currentWeapon : int; private var slot1 : GameObject; slot1 = GameObject.Find("GunTest"); private var slot2 : GameObject; slot2 = GameObject.FindWithTag("Secondary"); private var slot3 : GameObject; slot3 = GameObject.FindWithTag("Special");
var groundweap : GameObject; //groundweap = GameObject.FindWithTag("Pickupable"); private var playerWeapon : GameObject; playerWeapon = groundweap.GetComponent(PickUp).playerweap; private var nearweapon = false; private var groundWeapon1 : GameObject; groundWeapon1 = slot1.GetComponent(Gun).dropweap; private var groundWeapon2 : GameObject; groundWeapon2 = slot2.GetComponent(Gun).dropweap; private var groundWeapon3 : GameObject; groundWeapon3 = slot3.GetComponent(Gun).dropweap; private var groundpickup = groundweap.GetComponent(PickUp);
function Update() {
//var gun = currentWeapon.GetComponent(Gun); // Slot key commands if (Input.GetButtonDown("Slot1") || Input.GetButtonDown("Slot2") || Input.GetButtonDown("Slot3")) { SwitchSlot(); }
if (nearweapon && Input.GetButtonDown("Pickup")) {
PickupSlot(); }
}
function SwitchSlot() {
if (Input.GetButtonDown("Slot1") && !slot1.active) { slot1.active = true; slot2.active = false; slot3.active = false; currentWeapon = 1;} if (Input.GetButtonDown("Slot2") && !slot2.active) { slot1.active = false; slot2.active = true; slot3.active = false; currentWeapon = 2;} if (Input.GetButtonDown("Slot3") && !slot3.active) { slot1.active = false; slot2.active = false; slot3.active = true; currentWeapon = 3;} }
function PickupSlot() {
if (groundpickup.slot==1) { if (slot1==0) { Get();} if (slot1==1) { Drop(1); Get();} } if (groundpickup.slot==2) { if (slot2==0) { Get();} if (slot2==1) { Drop(2); Get();} } if (groundpickup.slot==3) { if (slot3==0) { Get();} if (slot3==1) { Drop(3); Get();} } }
function OnCollisionEnter(collision : Collision) {
if((collision.gameObject.groundweap) && (nearweapon == false)) { nearweapon = true; } } function OnCollisionExit(collision : Collision) { if((collision.gameObject.groundweap) && (nearweapon == true)) { nearweapon = false; } }
function Drop(slotdrop : int) {
if (slotdrop==1) { Destroy(slot1); Instantiate(groundWeapon1, transform.position, transform.rotation);} if (slotdrop==2) { Destroy(slot2); Instantiate(groundWeapon2, transform.position, transform.rotation);} if (slotdrop==3) { Destroy(slot3); Instantiate(groundWeapon3, transform.position, transform.rotation);} }
function Get() {
Instantiate(playerWeapon, transform.position, transform.rotation); }
The PickUp script is just declaring the 2 variables mentioned in this script. I looked through all my scripts w/ weapons and couldn't find anything wrong, but is my human mind compensating for something lacking in my script that Unity just doesn't get?
Your answer
Follow this Question
Related Questions
FPS Weapon inventory script help 0 Answers
switch weapons script not working? 2 Answers
Help needed in changing weapons 2 Answers