How to make 2 different actions for the same input?
Hello, I am developing the following action: running motion while the button "B" player is pressed. And run the motion scrolling while the same "B" button is just pressed without being kept. The error occurs when you hold "B" pressed and the player performs the action to run normally, but when releasing the button it automatically runs the animation scrolling. Example of games DarkSouls and Bloodborne where the same button performs different actions. I thank the attention and help.
Answer by Dibbie · Sep 27, 2016 at 04:59 PM
Easiest way is to use a "count" variable, and the GetKeyDown and GetKey actions.
GetKey, is your "while held down" action, and GetKeyDown, only occurs once, at the time the key is pressed down, but not held.
For example (C#):
int count = 0;
if(input.GetKeyDown(KeyCode.B)){
count++; //add 1 to count every time the button is pressed - so if they hold down the key, count will be 1 regardless, and we can use a if-statement in the GetKey event, to make sure either GetKeyDowns action or GetKeys action is ever performed
//perform single-press action
}
if(input.GetKey(KeyCode.B)){
//perform secondary action
}
if(input.GetKeyUp(KeyCode.B)){
count = 0; //reset count every time the button is released
}
Answer by carlos-udi · Sep 27, 2016 at 11:39 PM
Thanks for the great help, I managed to adapt and deploy the code with great success in my project.
Your answer
Follow this Question
Related Questions
Movement input problem? 1 Answer
2D Object following the cursor without glitchyness at the center 0 Answers
Game Object jumps to z-axis 0 Answers
Jump is higher then normal 0 Answers
Car coding help 0 Answers