- Home /
Forcing GetKeyDown by script.
Hello everyone,
I would like to know if there's a way to force the player to press a determinate key by script, for example, i want the player to automatically press "c" when he is dead, just want to know is this is possible.
Thanks in advance.
Answer by Linus · Jun 26, 2013 at 01:54 AM
As darthbator said, you do not actually simulate an input, you just do what would be done if the key was pressed. But here is an example on how you could do it.
//When the player dies, set this variable to true
var playerJustDied : Boolean = false;
function LateUpdate() {
//If c is pressed, or player just died
if(Input.GetButton("c") || playerJustDied){
keyCpressed();
//Set it to false again, so c is not pressed over and over
if(playerJustDied)
playerJustDied = false;
}
}
}
function keyCpressed(){
Debug.Log('Do what should be done when c is pressed or player died');
}
What I would normally do ins$$anonymous$$d of this example, is simply call the keyCpressed() as soon as the player is declared dead. As suggested by darthbator
Thanks Linus, this piece of script helped me a little bit. thank you so much.
Answer by darthbator · Jun 26, 2013 at 12:09 AM
I'm a little confused by the phrasing of the question. Do you want to script to provide the input or do you want to only continue if the user supplies the desired input???
If you don't desire the player to actually give the game input you shouldn't use the input system at all. For example in this case I would just call the method invoked by pressing C after the players "deathEvent" or as it's last instruction.
Thanks for you reply darthBator.
i just want to force the input to press "c" automatically whenever the character is dead, even if the player is not pressing anything. Since the Input.Get$$anonymous$$eyDown function is reading only, i don't know how i'm gonna do it. ^)
You do not need to use the input system. If pressing "c" fired a weapon and you wanted that weapon to fire via script (meaning without the player pressing "c"), you would do so via the script.
You could do this via variables and if statements (make variable = 1, if player dies add one to the variable, if variable = 2 then do whatever "c" was supposed to do than subtract one) or more simply by using a boolean and toggling between false and true.