Hi everyone glad you are here any help will be appreciated any how lets get to my questions I saw a lot of ways to do this in the forums but I want to make sure this will fit in my game and ideas for it aswell.
Currently I want to make a way to check your magazine to see how many rounds are in your gun by pressing a key twice preferably the R key. My weapon script has a reload function that uses the R key as well which is why I wanted to go with pressing R twice to check the magazine any ideas on how to do this? Any help will be appreciated here is my current code for reloading which I presume will have to be changed since I will be pressing R twice and make it think that I am not pressing it once if that makes sense.
I’m confused how you want to handle the first press of the R key. Do you want the reload to start, and then on the second press within a certain time frame it does the mag ammo check mid-reload? Or do you want a reload from the first press to wait until it is no longer possible for a second press to result in the ammo check (a check for double R has timed out), and only then start the reload?
Hi!
What I would do is storing a variable that defines the delay required to consider it a double-press, instead of two single presses.
Something like 0.25f could do it.
Then, I would go for something like this:
float delayBetweenPresses = 0.25f;
bool pressedFirstTime = false;
float lastPressedTime;
void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
if (pressedFirstTime) // we've already pressed the button a first time, we check if the 2nd time is fast enough to be considered a double-press
{
bool isDoublePress = Time.time - lastPressedTime <= delayBetweenPresses;
if (isDoublePress)
{
Debug.Log("check for ammo");
pressedFirstTime = false;
}
}
else // we've not already pressed the button a first time
{
pressedFirstTime = true; // we tell this is the first time
}
lastPressedTime = Time.time;
}
if (pressedFirstTime && Time.time - lastPressedTime > delayBetweenPresses) // we're waiting for a 2nd key press but we've reached the delay, we can't consider it a double press anymore
{
// note that by checking first for pressedFirstTime in the condition above, we make the program skip the next part of the condition if it's not true,
// thus we're avoiding the "heavy computation" (the substraction and comparison) most of the time.
// we're also making sure we've pressed the key a first time before doing the computation, which avoids doing the computation while lastPressedTime is still uninitialized
Debug.Log("reload");
pressedFirstTime = false;
}
}
Of course, this will ONLY process the single key press after the said delay, so keeping a short delay is better:
to ensure the user really wanted to do a double press, and not just two single presses in a short period of time.
to process the single key press faster (since we can only process it after making sure it can’t be a double press)
Please note that this code actually answers the 2nd hypothesis of @Joe-Censored , which is the one I assumed was the one you wanted.
I hope the code is clear enough! If not, don’t hesitate to ask
P.S: as a side note, please don’t use StartCoroutine with the name of your coroutine. It is a very bad practice because you’ll have a bunch of errors if ever you rename your coroutine later. Instead of StartCoroutine("MyCoroutine"), prefer: StartCoroutine(MyCoroutine()).
This is also better because you can use arguments just like with a regular function: StartCoroutine(MyCoroutine(arg1, arg2)).
Hey sorry If I said it wrong basicly if you press R and don’t press again for example within .50ms then it will reload, but if you press again it will check the magazine and not reload.
Thank you very much this is the exact answer I was looking for and thanks for the tip with my Coroutines I have recently started and am trying to improve have a good rest of your day.