- Home /
If you hold down button *BUTTON*, speed boost.
Hello. I'm working on my game and I'm new to scripting.Like, just learned a few things with the help of Jonathan Weinberger and I'm trying to make my own game now.
So I have a small problem. I have the player movement script and I want that, when I hold down the button Left Alt, add +10 speed while holding it down but just once, not every frame and when I release the button, get the normal value again.Here's the script.
I have a public float called playerSpeed and it's value is 7, I thought this would do the job but it's not working like I wanted to.
----------------------------------------------------------|
if(Input.GetKeyDown(KeyCode.LeftAlt)){ |
playerSpeed += 10; |
} |
|
else if(Input.GetKeyUp(KeyCode.LeftAlt)){ |
playerSpeed -=10; |
} |
--------------------------------------------------------|
So what I want to happen when I hold Left Alt down is, speed +10, if you release the Left Alt, speed back to 7. What should I do? I used GetKeyUp there because I think that's when you release the button (or am I wrong?). Help me please, thanks.
Answer by Angellify · Jul 29, 2014 at 07:02 PM
Alright what I did was to make a new script and copy the script there, I made the playerSpeed float static and added this code.
void Update () {
if(Input.GetKeyDown(KeyCode.LeftAlt)){
playerMovement.playerSpeed += 10;
}
else if(Input.GetKeyUp(KeyCode.LeftAlt)){
playerMovement.playerSpeed -= 10;
}
}
Problem is , if I press Alt the ship goes down for some reason, why?
Never $$anonymous$$d, I got it working. I wasn't paying attention and seems like I had another Get$$anonymous$$eyDown with "Left Alt" that was making the ship go down, thanks anyways.
Your answer