Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 maheerali · Jun 18, 2017 at 06:07 AM · 2drotationinput.getkey

How to detect release of GetKey?

The object doesn't come to 0,0,0 rotation as Key is released. But it come to 0,0,0 when i press key again.

 using UnityEngine;
 using System.Collections;
 
 public class Kicker : MonoBehaviour {
     Vector3 rotationEuler;
     public float kickspeed;
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         
         if (Input.GetKey (KeyCode.Space)) {
 
                 rotationEuler += Vector3.forward * kickspeed * Time.deltaTime; //increment 30 degrees every second
                 transform.rotation = Quaternion.Euler (rotationEuler);
             
         }
         
         else {
             
             Debug.Log("Released");
             rotationEuler = new Vector3(0,0,0);
         }
         
         
     }
 
 }




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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by RhisisLS · Jun 18, 2017 at 09:06 AM

Coroutines brother!

 public class Kicker : MonoBehaviour {
     //Speed of Kick
     public float KickSpeed;
 
     //Speed of Kick Returning
     public float ReturnSpeed;
 
     //Angle of Kick
     public float KickAngle;
     
     // Update is called once per frame
     void Update () {
         if (Input.GetKeyDown(KeyCode.Space))
             StartCoroutine(StartKick());
     }
 
     IEnumerator StartKick()
     {
         //Total Angle kick has moved from the Start
         float TotalAngleMoved = 0f;
 
         //Reference to the Updated Rotation of our Kick
         Vector3 RotationRef = transform.eulerAngles;
 
         //True if the Kick has not surpassed the wanted KickAngle
         while (TotalAngleMoved < KickAngle)
         {
             //Get the new Rotation of the Kick 
             float AngleMovedThisFrame = KickSpeed * Time.deltaTime;
             TotalAngleMoved += AngleMovedThisFrame;
             RotationRef.z += AngleMovedThisFrame;
 
             //Set the new Rotation on our Transform
             transform.eulerAngles = RotationRef;
 
             yield return null;//Keep Looping every frame
         }
 
         //-- We have finished Kicking --
 
         //Makes sure that the Kick Angle does not exceed
         RotationRef.z -= (TotalAngleMoved - KickAngle); //Deducts the extra angle
         transform.eulerAngles = RotationRef;
 
         //You can have a delay before it returns to its position if you want
         //yield return new WaitForSeconds(1f);
 
         //You can wait until the Key is released before it returns to its position
         while (!Input.GetKeyUp(KeyCode.Space))
             yield return null;
 
         //-- Time to start putting it down --
 
         //Reset the TotalAngleMoved
         //Use this like the previous while loop, but we're just moving it down
         TotalAngleMoved = 0f;
 
         //True if the Kick has not returned to its original Angle
         while (TotalAngleMoved < KickAngle)
         {
             //Get the new Rotation of the Kick 
             float AngleMovedThisFrame = ReturnSpeed * Time.deltaTime;
             TotalAngleMoved += AngleMovedThisFrame;
             RotationRef.z -= AngleMovedThisFrame; //Negative this time because we're going backwards
 
             //Set the new Rotation on our Transform
             transform.eulerAngles = RotationRef;
 
             yield return null;//Keep Looping every frame
         }
 
         //-- We have finished Returning --        
         
         //Makes sure that the Return Angle does not exceed
         RotationRef.z += (TotalAngleMoved - KickAngle); //Adds the extra angle
         transform.eulerAngles = RotationRef;
 
     }
 }
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 Squeekpro107 · Jun 18, 2017 at 06:11 AM

use input.GetKeyUp

Comment
Add comment · Show 2 · 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 Squeekpro107 · Jun 18, 2017 at 06:16 AM 0
Share

Can you explain what you are trying to do maheerali so we can get a better idea of how to fix it

avatar image maheerali Squeekpro107 · Jun 18, 2017 at 07:35 AM 0
Share

I want to make a game like sports head football. I have 2d sprite of foot/leg. When I press space it rotate to 90 in some time.90 should be max. If space in still pressed so z axis rotation should be 90. and if space in released so the rotation should come back to 0 in some time.

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

134 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D Projectile Not Firing Based on Rotation 1 Answer

object rotates toward mouse? 2D Top Gameplay 6 Answers

Angle to Rotation 2 Answers

Side scroller - rotate object towards player. 1 Answer

Start Coroutine after other has finished 4 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