- Home /
How to change pace by pressing different buttons once?
Hello. I have been looking around for hours, but I can't find what I am looking for, so now I seek help.
I want to be able to change pace on my character by using the keybindings "1, 2, 3 and 4", and only have to press the buttons once to change pace(with other words I don't want to have to hold the button down until I want a new pace, just one touch).
Stop: 1
Walk: 2
Trot: 3
Sprint: 4
^ that is what I had in mind.
I want to control where the character is heading with the arrow keys, but control the pace with 1, 2, 3 and 4.
Every tip is appreciated: tutorials, examples, etc.. :)
why not just have a speed variable that changes according to those 1-4 keys?
Answer by Veity · Jul 26, 2014 at 09:49 PM
Based on what you wrote i'd guess your looking for something like this:
int speed = 1;
// Update is called once per frame
void Update ()
{
if(Input.GetKeyDown("1"))
{
speed = 1;
}
if(Input.GetKeyDown("2"))
{
speed = 2;
}
if(Input.GetKeyDown("w")) //sorry dont remember the arrow key bindings
{
transform.Translate(Vector3.forward);
}
//Alternativly you may want 2 butons one to increase speed as long as the button is held down
//and one to decrease speed as long as it is held down
if(Input.GetKey("1"))
{
//Increases speed by 1 every second.
speed += Time.deltaTime;
}
if(Input.GetKeyDown("2"))
{
speed -= Time.deltaTime;
}
}
I haven't tested this method but looking at it now if you went for option two (increase over time) i think you would have to change speed to a float.
Your answer
Follow this Question
Related Questions
Slowing Down Rotation Speed 1 Answer
Changing Cameras With One Button 4 Answers
How do I change my speed based on certain events? 0 Answers
How To Make This To Work? 2 Answers