- Home /
Weapon not appearing
Hi!
I have made this WeaponSwitch script for my players in my game. When I try to switch weapon, it doesn't appear. This has just been tested with secondary and primary weapon. There is no problem with the primary but when I switch to secondary, it doesn't appear. It has the exact same properties as the primary one.
Here is my script:
void Start () {
primaryWeapon.SetActive(true);
secondaryWeapon.SetActive(false);
meeleWeapon.SetActive(false);
anim = GetComponent<Animator>();
}
void Update () {
if(slot > 1){
slot = 0;
}
if(slot < 0) {
slot = 1;
}
if(Input.GetAxis("Mouse ScrollWheel") < 0){
slot -= 1;
}
if(Input.GetAxis("Mouse ScrollWheel") > 0){
slot += 1;
}
if(slot == 0){
primaryWeapon.SetActive(true);
secondaryWeapon.SetActive(false);
meeleWeapon.SetActive(false);
}
if(slot == 1){
primaryWeapon.SetActive(false);
secondaryWeapon.SetActive(true);
meeleWeapon.SetActive(false);
}
if(slot == 2){
primaryWeapon.SetActive(false);
secondaryWeapon.SetActive(false);
meeleWeapon.SetActive(true);
}
}
If you know what the problem is, please tell me. Thanks.
Update is called every frame, meaning you are probably scrolling past the object because of how fast it is drawing the frame, this means that it will set the secondary weapon visible, for a frame, then because the input stays, it then switches straight to the melee, you will need to have some sort of threshold / delay so that you don't skip past weapons so fast.
Answer by snarf · May 11, 2014 at 06:41 PM
Try this (untested)
private float comulatedWheel = 0.0f;
void Update () {
float wheel = Input.GetAxis("Mouse Scrollwheel");
comulatedWheel += wheel * 0.1f;
if (Mathf.Abs(comulatedWheel) > 1.0f) {
slot += Mathf.Sign(comulatedWheel);
comulatedWheel = 0;
}
if(slot == 0){
primaryWeapon.SetActive(true);
secondaryWeapon.SetActive(false);
meeleWeapon.SetActive(false);
}
else if(slot == 1){
primaryWeapon.SetActive(false);
secondaryWeapon.SetActive(true);
meeleWeapon.SetActive(false);
}
else if(slot == 2){
primaryWeapon.SetActive(false);
secondaryWeapon.SetActive(false);
meeleWeapon.SetActive(true);
}
else {
slot = 0;
}
}
Your answer
Follow this Question
Related Questions
Weapon Selector 1 Answer
Xsi to unity animation problems 0 Answers
need help with [] 1 Answer
FPS - Gun Delay while Rotating 0 Answers
GUI does not show my current ammo 0 Answers