- Home /
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);
}
}
}
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;
}
}
Answer by Squeekpro107 · Jun 18, 2017 at 06:11 AM
use input.GetKeyUp
Can you explain what you are trying to do maheerali so we can get a better idea of how to fix it
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
Follow this Question
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