How to detect key release?

Hello

I have been trying to find how to detect a key being released. What I am attempting right now is that whenever the left shift key is pressed the players speed will increase and when let go it would revert back to the normal walking speed.

function Update() {
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded) {

moveDirection = Vector3(0, 0,
Input.GetAxis("Horizontal"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed; 

print("Speed: " +speed); //Speed check

if(Input.GetKey ("left shift"))
    {

    speed = 5.0; 
}

I have been looking at EventType.KeyUp but haven't been able to get it working properly. Is that the right approach? Can someone direct me in the right direction.

Thanks for the time and help!

2 Answers

2

http://unity3d.com/support/documentation/ScriptReference/Input.GetButtonUp.html

http://unity3d.com/support/documentation/ScriptReference/Input.GetKeyUp.html

Have you tried putting an else after that if? Something like this:

if(Input.GetKey ("left shift"))
{
   speed = 5.0; 
}
else 
 {
   //speed equals whatever your default is
 }

No Problem man.

It's not good practice to assign things unnecessarily. Better to change the speed on press and release.