My method is called more than once.
So basically in my game to drop armor that's on you press Q and Shift and when you press it my dropping bool turns true so my armor drop method will only be called once but its not working it still gets called 2 or more times heres the code.
public Helmet HeadGear;
public Leggings PantsGear;
public ChestPlate ChestGear;
public Boots ShoeGear;
public bool ArmorDropping;
if (Input.GetKey(KeyCode.Q)&& Input.GetKey(KeyCode.LeftShift))
{
ArmorDrop();
ArmorDropping = true;
}
void ArmorDrop()
{
if (ArmorDropping == true)
{
if (HeadGear != null)
{
HeadGear = null;
ArmorDropping = false;
}
else
{
if (ChestGear != null)
{
ChestGear = null;
ArmorDropping = false;
}
else
{
if (PantsGear != null)
{
PantsGear = null;
ArmorDropping = false;
}
else
{
Debug.Log("No Armor on");
ArmorDropping = false;
}
}
}
}
}
Sorry code came out wrong public Helmet HeadGear; public Leggings PantsGear; public ChestPlate ChestGear; public Boots ShoeGear; public bool ArmorDropping;
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.Q)&& Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftShift)) { // Get help for this ArmorDrop(); ArmorDropping = true;
}
void ArmorDrop() { if (ArmorDropping == true) { if (HeadGear != null) { HeadGear = null; ArmorDropping = false; } else { if (ChestGear != null) { ChestGear = null; ArmorDropping = false; } else { if (PantsGear != null) { PantsGear = null; ArmorDropping = false; } else { Debug.Log("No Armor on"); ArmorDropping = false; } } } } }
Answer by hexagonius · Oct 22, 2016 at 07:36 PM
GetKey will return true for every frame that's passed since you pressed the key. Use GetKeyDown instead, which ensures, that only the press is used.
Furthermore, the boolean ArmorDropping is useless, you can remove it, it's set to true "after" calling the method checking it, so the first call always returned, but since you used GetKey it worked the second time :)
But when i use keydown its hard to press Q and Shift in the same frame
SHIFT is a so called modifier, like CTRL and ALT. Use Get$$anonymous$$ey() for modifiers, which checks whether the key is pressed "at the moment". Use Get$$anonymous$$eyDown() for other keys to check if the user started pressing it "right now".