- Home /
Keycode else
Hello, I got this script that changes the size of an object when you press Q. It's C# btw.
if (Input.GetKeyDown(KeyCode.Q)) { Vector3 scale = transform.localScale; scale.y = 1F; transform.localScale = scale; }
I would like to change the size to 2F when you press Q again, how is that done?
Thanks, Andreas.
Answer by aldonaletto · Nov 11, 2012 at 01:08 PM
Well, the desired behaviour is unclear. What if Q was pressed a 3rd time? Should the scale return to 1F? And is 2F the original scale?
Supposing that you want to toggle between the original scale and half its size each time Q is pressed, you could use something like this:
// declare these variables outside any function:
float orgScale;
bool smallScale = false;
void Start(){
orgScale = transform.localScale.y; // save the original scale
// rest of original Start code
}
void Update(){
if (Input.GetKeyDown(KeyCode.Q)){
smallScale = !smallScale; // toggle current scale state
Vector3 scale = transform.localScale;
if (smallScale){
scale.y = orgScale * 0.5f; // set scale to 50% of original value
} else {
scale.y = orgScale; // restore original value
}
transform.localScale = scale;
}
// rest of original Update code
}
Thanks, it's such a script that I needed, the only problem is just that, if I use it in my Character Controller, it falls through the terrain (It should work as a "Crouch" script)
Your answer
Follow this Question
Related Questions
IF - ELSE Issue! (Help! I'm new to this) 1 Answer
Returning Which Key Was Pressed 1 Answer
OnTriggerEnter if else 2 Answers
How do I get my door to work? 1 Answer
How to do multiple 'if' statements for multiple different actions 4 Answers