- Home /
event for key release
is there any event for to detect when i release any key.when i release the left arrow or right arrow key in key board. when i press uparrow the character should move forward in the ice skating game.when i release the uparrow the character should move forward a little bit due to ice effect and gradually its speed should deceases and stop the movement.When i press the uparrow the character should move forward .
var smooth = 1.0; var tiltAroundZ ; var tiltAroundZ1 ; var target1 ; var target ; var force = 10.0; var offset = 1.0;
function Update ()
{
//left movement of the character ///
if(Input.GetAxis("Horizontal") < -.1 )
{
tiltAroundZ = Input.GetAxis("Horizontal") * ( - 45.0); //to tilt the character at 45 degree
target = Quaternion.Euler (0, 0, tiltAroundZ);
transform.rotation = Quaternion.Slerp(transform.rotation, target,Time.deltaTime * smooth);
transform.Rotate(Vector3.up * -0.2); // to rotate the character a little bit
}
else //to brig back the character to normal position after the tilt
{
tiltAroundZ = Input.GetAxis("Horizontal") * ( 0);
target = Quaternion.Euler (0, 0, tiltAroundZ);
transform.rotation = Quaternion.Slerp(transform.rotation, target,Time.deltaTime * smooth);
}
//right movement of the character//
if(Input.GetAxis("Horizontal") > .1 )
{
tiltAroundZ1 = Input.GetAxis("Horizontal") * (-45.0);
target1 = Quaternion.Euler (0, 0, tiltAroundZ1);
transform.rotation = Quaternion.Slerp(transform.rotation, target1, Time.deltaTime * smooth);
transform.Rotate(Vector3.up *0.2 );
}
else
{
tiltAroundZ1 = Input.GetAxis("Horizontal") * (0);
target1 = Quaternion.Euler (0, 0, tiltAroundZ1);
transform.rotation = Quaternion.Slerp(transform.rotation, target1, Time.deltaTime * smooth);
}
if(Input.GetKey(KeyCode.UpArrow)) // to move forward the character
{
transform.Translate(0,0,1);
}
else //this is to move the chacterter to little forward when the player release the uparrow due to ice effect
{
transform.Translate(0,0,0.3);
}
if(Input.GetKey(KeyCode.DownArrow)) {
transform.Translate(0,0,-0.1);
}
}
here i want the character to move a little forward direction when player release the uparrow due to skating effect and gradually decrease the character speed and make him stop(stand still).another need is when user press the uparrow the player speed should increases gradually like in cars when we start the car the speed is low as we press the uparrow the speed gradually increase and go fast.
Answer by taoa · Mar 31, 2011 at 09:16 AM
Input.GetKeyUp is what you're looking for! :)
Check this page out for more info: http://unity3d.com/support/documentation/ScriptReference/Input.html
Answer by Goodie848 · Jan 31, 2016 at 02:33 PM
Try this :
void OnGUI(){ if( Event.current.type.Equals(EventType.KeyUp) && (Event.current.keyCode == KeyCode.A || Event.current.keyCode == KeyCode.S || Event.current.keyCode == KeyCode.D) ) { //print("Stop releasing A,S or D"); } }
Answer by JoshuaMcKenzie · Jan 31, 2016 at 03:52 AM
There's no event for key release "event" per se, at least none that I could find through Unity. Unity's input system clears input data and rebuilds it each frame. My guess is because of all the complexity that the Input Class is likely handling under the hood for cross-platform support makes event-driven input...not viable. You can't, for example, attach an event handler to run once a key is entered, right out of the box. you have to poll every frame, usually in Update(), like you are doing for Input.GetAxis() in your example.
Input.GetKeyUp() will allow you to detect certain key releases, but its specific to PC.
Input.GetButtonUp() on the other hand allows you to detect multiple keys and is cross-platform supported. for example I have "ws" (from "wasd") tied with matching arrow keys and gamepad joysticks for the "vertical" input group, and I can simply check a single Input.GetButtonUp() to test all those inputs
There are events for key release, they're not normally recommended because they can be slow and hard to use but they exist. For every UnityGUI Event, OnGUI is called and you can get the current event.. To check if the event is a key release you can check the type. A script that checks the event for key release would look like this
public class ExampleClass : $$anonymous$$onoBehaviour
{
void OnGUI()
{
if(Event.current.type == EventType.$$anonymous$$eyUp || Event.current.type == EventType.$$anonymous$$ouseUp)
{
Debug.Log("$$anonymous$$ey/$$anonymous$$ouse up event detected");
}
}
}
Your answer
