c# speed myself up when a button (string) is pressed
I want my character to speed up when I press a button that is set by a string, something like this
public class Racer : MonoBehaviour {
public float speed;
public string speedButton;
private bool spedUp
void OnTriggerEnter(Collider other) {
if (Input.GetKeyDown(speedCheat))
{
if (spedUp)
{
speed = 15;
spedUp = false;
}
else
{
speed = speed + 10;
spedUp = true;
}
}
Yes I did make rigidbody and all that stuff, my speed float works just fine. This code is the one that just doesn't work, and I don't know what do I need to do in order to press a button that I set by a string in my Inspector. Can someone fix this up for me?
Answer by LazyElephant · Mar 14, 2016 at 11:59 PM
Both the OnTriggerEnter function and the GetKeyDown function only happen in the single frame in which they became true. So, in order for your if statement to be true, the player would have to push the speedCheat key in the exact same frame that they enter the trigger.
The easiest solution I can think of, without knowing exactly what you're trying to do, is to change GetKeyDown to GetKey. GetKey will be true as long as you're holding down the key, so if you hold the key down while you go through the trigger, your if statement will be true.
Your answer
Follow this Question
Related Questions
How can I increase the speed gradually of each instantiated clone. 0 Answers
Vector3 magnitude broken? 1 Answer
Using one key only, change bool to true or false depending on it's current value 2 Answers
Adding Speed 0 Answers
How do I save separate keycodes presses at same time and store for use later in update? 0 Answers