- Home /
Question by
raptorkwok · Jan 16, 2014 at 02:28 AM ·
gameobjectkey pressed
Change scale of GameObject on key action
I tried to change scale of an on-scene GameObject using the following code in Update()
if (Input.GetKeyDown (KeyCode.DownArrow)) {
gameObject.transform.localScale = new Vector3 (1, 0.5f, 1);
} else {
gameObject.transform.localScale = new Vector3 (1, 1, 1);
}
However, the GameObject
(which is a primitive cube) flickers when down arrow is pressed. I expect the cube will keep 50% height when the down arrow is being pressed.
What did I miss out this time?
Comment
Best Answer
Answer by Eric5h5 · Jan 16, 2014 at 02:29 AM
GetKeyDown returns true for the single frame that the key is pressed down. Presumably you meant GetKey instead.
Answer by b1run · Jul 24, 2017 at 10:24 AM
if (Input.GetKeyDown (KeyCode.DownArrow)) { gameObject.transform.localScale = new Vector3 (1, 0.5f, 1); } if (Input.GetKeyUP (KeyCode.DownArrow)) { gameObject.transform.localScale = new Vector3 (1, 1, 1); }
try this @raptorkwok