- Home /
The question is answered, right answer was accepted
How can I scale an object with keyinput and return old scale when I do not press like Mario crunch
The thing is I only want him to scale this 1 time when I key is pressed and only y should change.
void Update () {
if (Input.GetKey("down"))
{
transform.localScale = new Vector3(0, 0.25f, 0)* Time.deltaTime;
}
Answer by andracer108 · Jan 24, 2018 at 06:14 AM
Ok, so the answer is simple. You are only providing an "if" statement for what would happen when the user presses "down". But what would happen if the user releases it? How you wrote the code, nothing would happen. And this is because, as said before, you are only supplying an "if" statement to when the user is holding the button down.
To fix this, all you have to do is add an "else" statement after you close the "if". After you opened up the "else" statement within it, write the code to make the character have normal size again. Speaking of size, I think the size you're applying is wrong as well. You're making the x and z sizes 0 which would make your character disappear, which I'm assuming you don't want. Anyways, the code would look something like this:
void Update () {
if (Input.GetKey("down"))
{
transform.localScale = new Vector3(1f, 0.25f, 1f) * Time.deltaTime;
}
else
{
transform.localScale = new Vector3(1f, 1f, 1f) * Time.deltaTime;
}
}
Hope this helps! And if it did, kindly mark as answered. :)
Thanks alot but I have changed my project long time ago just saw that you answered :) I appreciate your help
It's no problem at all! I'm glad I helped. :) Good luck with your new project!