- Home /
How Can I Fight Button Mashing - Need To Slow Down Input Rate
Hi everyone,
I've got a platform fighting game that I've been doing this week as a sort of mini-jam, and I was wondering how I could delay inputs from the "punch" button. I have a basic trigger on my player that collides with the other player's trigger. When P1 uses GetKeyDown(KeyCode.X) while OnTriggerStay is happening, it causes -5 damage to the other player.
Often it registers twice (so -10 on one punch) and I suspect it's because I've made my timestep very small to help read collisions, thus registering two hits in the small time the player activates the key.
I want to set the punch keys to only register every .1 seconds, or something a bit quicker, so you can't just pound the key and do -50 damage in one second.
Any help would be appreciated!
Pat,Hi everyone,
I've got a platform fighting game that I've been doing this week as a sort of mini-jam, and I was wondering how I could delay inputs from the "punch" button. I have a basic trigger on my player that collides with the other player's trigger. When P1 uses GetKeyDown(KeyCode.X) while OnTriggerStay is happening, it causes -5 damage to the other player.
Often it registers twice (so -10 on one punch) and I suspect it's because I've made my timestep very small to help read collisions, thus registering two hits in the small time the player activates the key.
I want to set the punch keys to only register every .1 seconds, or something a bit quicker, so you can't just pound the key and do -50 damage in one second.
Any help would be appreciated!
Answer by pheash · Jul 24, 2012 at 09:43 AM
Hi, Instead of checking for GetKeyDown in your Update function check it in another function that gets executed every .1 seconds by using InvokeRepeating:
in your controller script:
function Update(){
if(playerIsControllable && !IsInvoking("CheckInputs")){ // if we are not already checking for inputs and the player is controllable (boolean that is true during the fight) we start checking for inputs every .1 seconds
InvokeRepeating("CheckInputs",0,0.1); //starts imediately to execute CheckInputs() every 0.1 seconds
}
if(!playerIsControllable){ // if the player is not controllable (eg during startup) we stop checking for inputs.
CancelInvoke("CheckInputs");
}
}
function CheckInputs(){
//put all your getKeydown stuff in here.
}
Let me know if you have any problems with that concept.
Thanks pheash: this occurred to me, though your structure works great, would have taken me a bit to figure out how to get it going. I haven't tried it yet, and I'll confirm it works, but thanks a bunch! There's no reason invokeRepeating can't be used many times per scene right? I've got powerup randomization scripts that go off every 10 seconds.
I think ideally you only invoke a certain function once. you can use if(!IsInvoking("function")) to check whether it isnt actually already invoking. but invokerepeating different functions shouldnt be a problem
Your answer
Follow this Question
Related Questions
Rotate an object with angle limit 1 Answer
C# inputString mouse Keys? 1 Answer
How to set Input Buttons 2 Answers
add button like escape or shift 1 Answer
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers