- Home /
Command for Holding a Key Down & Shift Key
Question 1: Command for Key Holding
hat is the command for creating an action while a key is being HELD as opposed to PRESSED. I want Shift to make my character run:
moveDirection = transform.TransformDirection(moveDirection); moveDirection = speed;var speed : float = 6.0;
var runSpeed : float = 20;
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
Input.GetAxis("Vertical"));
if (Input.GetButtonDown ("Shift")) {
moveDirection *= runSpeed;
}
else {
moveDirection = speed; } So, I can't get the "runSpeed" to stay active because it isn't happening upon the button being HELD as opposed to PRESSED (I think). #Question 2: Command for Shift Input what is the GetButtonDown ("Shift")) command for Shift, because I tried "Shift" and that wasn't working either. thanks!Answer by DaveA · Feb 07, 2012 at 01:42 AM
You detect shift, alt, and control in OnGUI, so I set 3 boolean vars, which I set accordingly in OnGUI off the Event.current.shift (for example). Then you can use those booleans in your Update function to see if those keys are held when another key is hit. You can also track the 'last state' of those booleans and compare them to see if shift, control, alt were pressed or released.
Now would be a good time to see if they fixed all the bugs: http://answers.unity3d.com/questions/57147/mac-keys-not-showing-up-or-very-strangely.html
Answer by Eric5h5 · Feb 07, 2012 at 02:04 AM
if (Input.GetButtonDown("Shift")) {
running = true;
}
else if (Input.GetButtonUp("Shift")) {
running = false;
}
moveDirection *= running? runSpeed : speed;
When using GetButton, you set the keys in the input manager. You'd need both right shift and left shift, so you can use those for the positive button and alt positive button.
What is the difference using Get$$anonymous$$eyUp vs GetButtonUp ?
Get$$anonymous$$eyUp is using $$anonymous$$eyCode, which you specify in your code. GetButtonUp is referring to a button that you have assigned in Edit>Input.
GetButtonDown is generally seen as better, as you can quickly change which key does what, but if you know for a fact that you're going to be using Shift the entire project, it won't make any difference.