- Home /
Switch statement not running on every frame
Hello there! This will probably be really obvious and simple but the switch statement I have implemented in my code does not exactly work 100%. All I'm trying to do is pick up a weapon, hold down a button to charge a throw then release the button to fire it (like throwing a spear). I have my methods for charging the throw and releasing (I think) but for now I'm just trying to get it to work with simple Debug.Log statements. As you can see down in my switch statement where it says "Debug.Log("CHARGING");" I want that to appear every frame as long as I hold down the button and as soon as I release said button it should print "FIRE" once.
As of right now it only seems to print out "CHARGING" once while I hold the button which isn't right at all. I know something probably isn't right with one of my statements but could anyone help debug my code and point out where I screwed up? This is my first post so any help is great, thanks!
Edit: All this code is running within the update function for a little extra info in case anyone thought this was why it wasn't working.
Code
if (playerOnWeapon)
{
switch (pickedUpWeapon)
{
case false:
if (Input.GetKeyDown(KeyCode.M))
{
pickedUpWeapon = true;
weapon.transform.parent = weaponGrabber;
weapon.transform.position = weaponGrabber.transform.position;
weapon.transform.rotation = weaponGrabber.transform.rotation;
weaponRB.isKinematic = true;
Debug.Log("You picked up the weapon");
}
break;
case true:
if (Input.GetKey(KeyCode.N)) //Holding down charge button
{
if (!isCharging)
{
isCharging = true;
Debug.Log("CHARGING");
}
}
else //No longer holding down the charge button
{
if (isCharging)
{
pickedUpWeapon = false;
isCharging = false;
Debug.Log("FIRE");
}
}
break;
}
}
Answer by YoucefB · Oct 15, 2017 at 02:23 PM
You just need to remove if (!isCharging) statement.
if (Input.GetKey(KeyCode.N)) //Holding down charge button
{
// once you hold N isCharging will switch to true..
// if (!isCharging) // meaning it will skip this part next time until its false again when you release N key.
//{
isCharging = true;
Debug.Log("CHARGING");
// }
}
Your answer
Follow this Question
Related Questions
How to call PlayerPrefs in an Update function without hogging memory? 0 Answers
Help with sound playing when a certain bool = false 1 Answer
Disable an if statement in update 3 Answers
What is the download size of updating from version 2019.3 to 2019.4.1f1? 0 Answers
void update working under conditions 1 Answer