tap and hold function to keep flying in c#
I am using this code to make a fly.
void Update () {
// Fly
if (Input.GetKeyDown(KeyCode.Space))
GetComponent<Rigidbody2D>().AddForce(Vector2.up * force);
}
But everytime i push and hold my space button the fly still fall like the simple press space button. Im just thinking if is it possible to have press and hold function to make it fly up? I dont have idea what should i start. Please share.
I forgot to tell, i dont use ground so actually the player keep falling, i added rigidbody to it and i want to add tap and hold function to make it fly, and keep fall if i release the tap.
Sorry for my english. I hope you understand what i mean
Answer by Landern · Aug 09, 2016 at 03:33 PM
GetKeyDown only get's the key in the current frame, from the documentation for GetKeyDown:
Returns true during the frame the user starts pressing down the key identified by name.
How with just GetKey it can be recognized as down per frame and from the documentation for GetKey:
Returns true while the user holds down the key identified by name. Think auto fire.
So use GetKey which should return true each frame it's held down.
Your answer
Follow this Question
Related Questions
Float speed not getting updated on FixedUpdate nor Updated and OnAnimatorMove 1 Answer
How to make soccer ball spin according to its velocity in 2D top down game 0 Answers
Shooting an instantiated missile the way a object is facing. 0 Answers
How to access the coordinate x y after normalized it by another variable? 0 Answers