Scale a GameObject up and down in size - 5

Unity
Transcript

English (Auto-generated)

Hi everyone in this cast. We'll work on some logic inside the update function. So if I play the game right now, nothing happens. So we're going to write some logic in the update function which is going to scale our three game object for every frame of the game. Now, the update function runs once every frame. And this is where usually game logic goes. Having a high frame rate means update gets called more often. And having a low frame rate means that the update method gets called less. Okay, so let's go to the code editor and we are working inside of the update method. So instead of just using any key press, we'll actually make it more specific and work around checking if the space key is pressed down on the keyboard. So first let us structure and if else conditional statement. And now what we're going to do is we are going to use the input class which provides an interface to the input system. And then we're going to use something called the get key down method. Okay. And this is going to take as an argument key code. Dark space and this will map directly to the space key on the keyboard. And key codes can be used to detect the any keyboard events like a key down and key up event. Okay. So if the space bar is pressed down then what we want to do is we want to call the function which we worked on earlier, which is called toggle scaling. So the toggle scaling function if you remember is responsible for starting and stopping the object from scaling Now we will check if the tree game object should scale. So if the value of the scaling up variable is true, then call the scale up function else. If the value of is a scaling up variable is false, then called the scale down function. And after this lead as a structure and if else key block. Sorry if I was block not key anyways, so here we're saying well if it's a scaling up right then we want to call the scale up a function. So if is scaling up is true then called it is a scale then call the scale up function else if is scaling up variables, value is false, then call the scale down function. Okay, now, before we move on let's talk about time, delta time. So time dot delta time is the amount of time seconds or milliseconds between each frame of the game. Now this is used for frame rate dependent calculations such as movement, timers etcetera. So that calculations will be the same in various frame rates across various devices. So what we're going to do is we are going to use time dot delta time to change alpha from 0 to 1 over time using delta time and scale speed. So we will say alpha is equal to time dot delta time. So time dot and then we say delta time. Right? And then we're going to say times a skill speed. So value of alpha is now going to be changed from 0 to 1 over time and skill speed. Okay, so moving on now we're going to use math F. Which is a static class, which will which you can use for math calculations. And we're going to use clamp which is a math function which clamps the value between zero and one and returns a value. So what we're going to do is we'll say, well alpha is equal to math s dot clamp uh Euro one. And we're going to clamp the value of alpha. So in case alpha goes above one, then we want to set the alpha the value of the alpha variable to one. And if the value of the alpha variable goes below zero, then we're going to set it to zero. Okay, now next what we want to do is we want to check if the tree game object has reached a max scale or min scale. Okay, So let's the structure and if else let's just use F. So what the condition out here is, well if alpha is one, right? So it's reached max value, right? And if you remember that max scale value is one F. So if it's reached its max scale Then it should start scaling down the condition for scaling down, is that is scaling up, variable should be false. So let's set it to false. And then we also want to reset the value of alpha to zero so that it can increment from 0 to 1 again for the scaling process. Okay so let's go ahead and control s to save and head back to the Unity editor. So it's compiling the script and okay so no errors and you see that the warning also went away. Now let's play the game. Okay? So now you can see that our three game object is indeed scaling up and down in a size, reaching the max scale of one F. And um in scale of zero. And if I press the space bar key now the three game object should stop this scale animation. Right? So we should just reach our max value which is not happening. So let us stop this and let us go back and see what is happening here. So um can get key down and uh key code space. So out here I just want to let's just say oops debug, right, so we can use a print or debug dot log and I can say something like okay space so I'm just checking what's happening here or I could just do key code. But anyways so now let me go back basically this is just going to log something for me, I'm not sure if it's gonna be helpful. Okay so debug dot log. So let's come back and see what is happening here. Um So if is scaling, let's see just check the game logic so if not should scale down. Is that it? If it should skill is equal to. Okay, this looks fine. So let's see what is going up. So if it's scaling up then yes. And else if this is false scale down, that looks fine to me. If alpha is um it's killing up. Mm hmm. That looks fine as well. Alpha is zero. Yes. Uh we are doing this fine. Okay, let's just double check all the code once again and let's see, let me try something. Let me just d a bug. Let's just say um can I do input dot get key down and just get the key code and then try this. Okay, so that is not that is something we cannot use out here. Well, why don't, maybe this is not like space, this is called something else. Let's just see something. It might not work. But Okay, true. Okay, so now this is telling us. Okay, so every time I press the space bar, it is indeed saying true, which means well, the space bar key was pressed down. So that's how I know. Well maybe there's something wrong with the game logic because out here I can see, well, you know when I'm pressing the space key, it is doing what it's supposed to do, which is say well yes space key was pressed, which is true. Okay, so which means now we have to very carefully check these lines of code. So if not should scale okay, this is what I did not put in and this is what needs to be put in. Okay. Good stuff. So now what I'm gonna do is I'm gonna put a condition. Okay? And what is the condition? Well, this is the condition. So we want to check if whether the tree game object should become bigger or smaller. So that means if scaling up is equal to true. Not Sorry. So we well, we already worked on that. What I'm trying to say is we want to check if the object should scale at all. Right. So if the value of a should scale variable is false. Then we want to ignore everything in this update function down below. Okay. So we need a condition here. So here I'm going to say if not should scale. Right? So if nothing should scale then. Well, what what do we want to do? We just want to return, which means exit out of this update method completely. Okay, so now if I go back um let's hope for the best. And Okay, space bar key, I'm gonna press that and there you go. So it is working. And if I press the space bar key once again, the animation should start. So basically what what we've done is we've worked on an animation which when the game plays by default is executed so the tree game object is scaling up and down, but it's also controlled by the user so the user can stop the animation on some key, press or key down, and then when they press the key again right, the animation reactivates.
109 Views 0 Likes 0 Comments

In this cast we will finish writing code inside the Update() method which will scale the GameObject for every frame of the game

Comment
Leave a comment (supports markdown format)