If Key is Continuously Pressed Unity
- Spaces
- Default
- Help Room
- META
- Moderators
-
- Topics
- Questions
- Users
- Badges
- Home /
- Help Room /
0
How to detect a keyboard key held down?
On every even click of the space bar, my gameobject translates on a 30 degree vector up and to the right. On every odd click of the space bar, my gameobject translates on a 30 degree vector up and to the left. How can I make is so that when the space bar is held down (rather than just clicked) the gameobject move straight up, on a 90 degree angle?
Thank you.
2 Replies
· Add your reply
- Sort:
2
Answer by yummy81 · Feb 13, 2018 at 10:54 AM
I'm not sure whether this is what you want, but try to play with this code. It's only for Spacebar key. With each press you move alternately left or right, and when you hold the spacebar a little bit longer (longer than timeSpan variable) then you move on a 90 degree angle:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class MyScript : MonoBehaviour { public float timeSpan = 0.3f; private bool even; private float time; private void Update() { if (Input.GetKey(KeyCode.Space)) { time += Time.deltaTime; if (time > timeSpan) Debug.Log("90 degree"); } if (Input.GetKeyDown(KeyCode.Space)) { if (even) { Debug.Log("30 degree vector up and to the right"); } else { Debug.Log("30 degree vector up and to the left"); } even ^= true; } if (Input.GetKeyUp(KeyCode.Space)) { time = 0f; } } }
1
Answer by UnityMooCow · Feb 12, 2018 at 07:05 PM
if (Input.GetKey("space")) { print("Space held"); }
Add this to your update function, replacing the print function with your code for moving. I don't know the translation at an angle code off the top of my head, but it sounds like you'll just need to replace the 30 with 90.
@Unity$$anonymous$$ooCow Thank you, but that code will run the second the key is pressed. I am looking to change the vector to 90 degrees when the key is held down rather than just pressed.
On every even click of a key or tap on the screen, we move up and to the right. On every odd click of a key or tap on the screen, we move up and to the left. I am looking to move 90 degrees up with no x or z movement when the key or screen is held down for an elongated period of time.
So if I understand this right, if you tap the spacebar, it will excecute code based on the press number, but if held for a short length of time, it will do something else
Every time the space bar is pressed, I increment a counter variable of type integer. Then, I take that counter variable and check to see if it is even or odd by using the modulo operator. In the event of an even click, the gameobject translates up and to the right. In the event of an odd click, the gameobject translates up and to the left.
What I am trying to implement is that while the space bar is held down (not just tapped) the gameobject move straight up at a 90 degree angle.
Thanks for your help.
Your answer
Welcome to Unity Answers
If you're new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.
Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.
Check our Moderator Guidelines if you're a new moderator and want to work together in an effort to improve Unity Answers and support our users.
Follow this Question
Related Questions
delacruzspeargons.blogspot.com
Source: https://answers.unity.com/questions/1467659/how-to-detect-a-keyboard-key-held-down.html
@piotrek_81 Thank you. Your code does indeed allow me to detect long presses. The problem is, the gameobject still translates on one of the oblique vectors until the timer variable gets above 0.3. I have even lowered it to 0.1, and it still moves ever so slightly left or write until the time limit is exceeded. You can see the code below inside Update():
@jamiecorkhill5 And what do you think about it now?
@piotrek_81 Thank you very much. The code is beautiful and works very well. However, there is one small thing which I am not sure is very fixable.
When you click the space bar (just a tap and not a long hold), sometimes, not every time, the gameobject moves straight ever so slightly just before moving on the oblique vector. Do you have any ideas?
I am most grateful for your help.
Show more comments