- Home /
FPS weapon swap is unresponsive (C#)
Okay, so I've been making a game and I set it so that every time I press Q it switches between the assault rifle and the pistol. The weird thing is that it only works like 1/3 of the time, so I end up mashing Q every time I want to switch weapons. I know that the problem isn't with the check "if player1controller.player1candoaction = true", because my shooting scripts & reload scripts both also have that check and they work just fine. Is there something wrong with the script?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Player1WeaponManager : MonoBehaviour
{
public GameObject weapon1Highlighter;
public GameObject weapon2Highlighter;
public GameObject Pistol;
public GameObject AssaultRifle;
public static int weapon;
public int weapontoggle;
public PistolDamage handgun;
public AssaultRifleDamage rifle;
void Start()
{
weapon = 1;
weapontoggle = 1;
}
void FixedUpdate()
{
if (Input.GetButtonDown("SwitchWeapon"))
{
if (Player1Controller.Player1CanDoAction == true)
{
if (weapon == 1)
{
weapon = 2;
weapontoggle = 2;
SwitchWeapon();
}
else
{
weapon = 1;
weapontoggle = 1;
SwitchWeapon();
}
}
}
}
void SwitchWeapon()
{
if (weapon == 1)
{
Pistol.SetActive(false);
AssaultRifle.SetActive(true);
rifle.enabled = true;
handgun.enabled = false;
weapon1Highlighter.SetActive(true);
weapon2Highlighter.SetActive(false);
}
if (weapon == 2)
{
Pistol.SetActive(true);
AssaultRifle.SetActive(false);
rifle.enabled = false;
handgun.enabled = true;
weapon1Highlighter.SetActive(false);
weapon2Highlighter.SetActive(true);
}
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How do you make a mouse sensitivity option? 0 Answers
Reload function in my script not working at all. No error messages in compiler. 4 Answers
Move gameobject pivot 6 Answers