Scale a GameObject up and down in size - 2
Transcript
English (Auto-generated)
Hi everyone in this cast. Let's start working by declaring a couple of public and private variables in the object scala's script. So let's open this script up and after the class declaration just above the start method, let's declare our first public variable which is going to be public. So float and it's going to be max scale is equal to one F. So max scale determines how large the object can be. And it's also used as the starting scale of the object at the beginning off the scene. And then we have public float, Mint Scale which is equal to zero F. And minimum scale. Mint scale determines how small the object can be. And then we have public float Scale speed, which is also equal to one f. So these are public variables. So let's control a safe and head back to the editor. And if I go ahead and select the tree game object in the inspector window to the right, you can see that max scale, mean scale and scale speed objects are visible in the editor and there, you know, you can edit the values here so you can do this for the public variables, but not for private variables. Now, let's head back and declare if you private variables. So we have private Sloat Alpha, Which is equal to 0f. So alpha determines the scale of the object, relative to the values of the max scale and main scale variables. So this value goes all the way from zero F to one and when the value is zero F, it means that the tree object scale is equal to men scale which is also zero F. When the value of alpha is one F, it means that the tree object scale is equal to max scale which is equal to one F. Here. And when the value of alpha is 0.5 F, it means that the three objects scale is at the halfway point between main scale and max scale. Okay, then we have another private variable which is of the bullion dera type and this is is scaling up and for now we're just going to leave it be as is and what this is going to do is it's going to determine if the object should be scaling up or down. Then we have another private bull. So private bull should squeal which is equal to true at the beginning. So this variable determines if the object should be scaling at all. So private variables. If we just, you know, save these, save a script. Right? And then I go back to my editor. You'll see that you cannot edit private variables in the unity editor. Okay, But you can for public variables right Now we're going to declare to more private variables which are going to be Victor three variables. So let's work on that. And let me just type this out before I explain it. So vector three. Re um not X. Sorry, re max scale is equal to not equal to we're gonna use the get method and it's going to return vector three uh 0.1 times max scale. Okay, so let's see what's happening here now. The variable V max scale is a vector three variable which represents three D vectors and points along the X, Y and Z axis. And if you remember in three D games we actually have three axes, we have X, Y and Z. And it's similar to a variable but you can add functionalities when getting or setting its value. So basically we're using Get method that's going to convert the float max scale up here to a vector three. And just as a note, vector three, one is similar to this 111, so this here record 3.1 just means victor three and then 11 and one so one on the X axis, one on the Y axis and Y on the z axis. Okay, so when we so we've converted um max scale to a vector three, we also have to do the same for our main scale. So vector three V. Men the skill and get and then we want to return vector three uh times men's skill. Okay, so and now we've converted the flow to main scale to a vector three out here. So control S. And a handy tip is like when we control s and we go back to our editor, we see there's actually there's no errors and we can play the game but nothing is going to happen as yet. So the next part we want to start working on the code to scale a tree up and down in size and then stop and restart the scaling animation on key. Press so back in our objects killer script. Let's go out here inside of the start method. Now, when the game starts, the tree is automatically scaled up and the code for this will be inside the start method. The start method runs once at the beginning of the scene. Right? And we want to set or get values or any references inside of the start method. So what we'll do is we'll say transform got local scale is equal to the max scale. So transform classes a collection of uh the position, rotation and scale of an object. And calling, transform would refer to the transform of the object that the script is attached to. So in our case it's the tree, game object and local scale out. So transform dot local scale out here is a vector three which refers to the scale of the object regardless of of the scale of the parent. So whatever the parents, scale might be, it doesn't matter out here, it's going to refer to the scale of the object record of the object, regardless of the scale of the parent. So we're setting the scale of this object which is the tree to be V max scale as the starting scale and v max scale if you remember is now a vector three variable, which represents three D vectors and points along the X, Y and Z axis. Alright, so let's control a safe and let's go here, right, and let's play. So no errors, but it's not scaling up and down um in size as yet. And before that, we're actually going to be working on some code inside of the update method here, right. Which is going to scale our tree up and down.