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.
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
}