Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by robertmathew · Mar 31, 2011 at 08:00 AM · eventkey

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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"); } }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image maccabbe · Jan 31, 2016 at 04:58 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Any way to key JUST keydown and JUST keyup events? 6 Answers

Can you trigger a function with an event in a base class conditionally? 0 Answers

Is there a smarter way to implement event sequencing? 1 Answer

Error in AnimationState.cpp Line: 307 1 Answer

checking for keyboard input with exceptions 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges