How to change Button input to Keyboard input?
Hi. I've just started learning to use Unity last month but currently have an assignment of building a game.
I face problem in changing button input to keyboard input. I followed this tutorial (https://www.youtube.com/watch?v=LJrc1NHY23w) for the spawning object part of my game.
In this tutorial, he uses button as a way to spawn object. For my game, I need the spawning part to be triggered by keyboard input.
This is the code Ive done now:
Please help. Thank you very much.
Answer by PizzaPie · Apr 29, 2017 at 03:41 PM
Well in this code you have an Input check for a keyboard key (call it button if you want). But you need to check inside a loop for that specific input, preferably an Update(). So to make this spawn an egg each time you press a specific button. Something like this :
void MakeRandomEgg()
{
if (Input.GetKeyDown(KeyCode.E))
{
//remove the if(spawnEgg)
// keep the rest as is
}
}
void Update()
{
MakeRandomEgg();
}
That will spawn an egg each time you press "E". Cheers.
Your answer
Follow this Question
Related Questions
How do I save separate keycodes presses at same time and store for use later in update? 0 Answers
How to detect Keycode.Return using an Android bluetooth keyboard? 2 Answers
Spawn objects from picture isnt working 1 Answer
Joystick buttons don't work 0 Answers
How do I spawn a List of Objects, so that there is a delay after each object that spawned? 0 Answers