Using Transform to move a GameObject

Transcript

English (Auto-generated)

Hi everyone in this cast, we'll move the cereal box to the left, right forward and backward when a user presses down on the left, right up or down arrow keys and heading back to the code editor. So we're taking off from where we left in the last cast, we're going to do this inside. A function called move. So let's declare a function called move with public, avoid move. And let me just comment out the closing curly base end of move. Okay, so the use of public and void key, which just means. So what it means that the function doesn't return any usable value and public means that this function can be accessed from anywhere in your script. So we've declared a function called move, which will handle the logic for moving the cereal box left, right up forward and backward. So inside this function let's go ahead and structure and if block. So I'm just going to structure an if block. Like so and once again we can write end off if Okay, now if someone is moving the left arrow key, we want to move the cereal box to the left and we can do that here with input. Got get keep down. And then this method takes as an argument key code dot left arrow. Mhm Okay. We use the get key down method off the input class. And this method takes as an argument the key code property which is mapped to the left arrow key and input dot get key down will return through once when the specified key is pressed down. Okay? So you press it and you release it. So when it's pressed down, once it's going to return true. So when we detect that the left arrow key is pressed, then we want to transform the cereal box by moving it to the left side on the screen. So let's go ahead and just test this part out till here by using a debug dot log statement. So out here we'll say debug dot blog and this takes some string as a value. So we'll just strike moving left and don't forget the closing semi colon control s to save. And now we expect to see something in our console and I'm just going to go ahead and comment this out in the update method because that's a lot of console messages. Okay, So now it's going to compile the script so there's no errors, which is good. And let's go ahead and click on console. Let me just clear this console and now click on play and no console message. So let me go ahead and press on the left hierarchy. Okay, Rachel. So it seems like I was not playing the game now I am. Okay, so now you see that start method has been called so we can see this in the console that message. Now, I'm going to press down on the left arrow key. Right? And hmm no message here. So let's see what's going on. So debug dot log moving left. Okay, So let's not worry about this right now. Um let's just work on getting the cube. Sorry, the cereal box to move and to do that. We're going to be using transform. So every game object has a transform component in reference to its parent or world coordinates. Okay, so for example, this cereal box out here has a transform camo component and you can see this out here right in the inspector panel, you can see the transform component. So coming back now we can use this component either through scripting or the unity editor interface from the inspector window like you just saw, we're going to be using our script, particularly inside the function move So transform stores the position, rotation scale of the game object to which the script is attached. So as properties we have the position property, rotation property and scale property and we want to move the cereal box to the left when the left arrow key is pressed down and we'll do this with So I'm still working inside of F. So transform Okay, so now I'm referencing a transform and this the position property of our transform component is equal to this dot transform. So whatever is the current value of the transform position. And Out here, I'm going to save class new vector three and I'll just explain this new vector three which takes as an argument -0.5 five F, zero F and zero F. Okay. And let's not forget to close off with a semi colon. Right? So here we're saying that the value of the position property is equal to the current position of the serious cereal box plus the new vector three direction that you're moving your cereal box to. So the vector three structure represents a 3D coordinate and it's most commonly found in the transform component of most game objects where it's used to hold the position and a scale. Now, since it's a three D coordinate, we're going to take as arguments X, Y and Z coordinates. So this is X. This is why. And then the last one here is Z. And since we only want to move left, we will pass in as an argument for x minus 0.5 F and zero and zero for the Y and Z coordinates. And F here stands for float and it's a data type used for decimal numbers. So we're only moving the cereal box a bit to the left when the left arrow key is pressed down. Now this function is not being called anywhere, which is why you did not even see this. Uh this debug dot log message. It's because we need to call the function. Yes, we've declared it but function this function is not a self invoking, right? So it's not just going to call itself, we need to determine when it's going to be called. So we want the game to check if the move method is called for every frame of the game. So let's call this function inside of the update method. There you go. So now we should have our um debug, debug message in the console and we'll also move the cereal box to the left. So let's go ahead and try the self to control asset to save and heading back. Um So it's compiling the scripts clear and let's play. Okay? So it was already playing. There you go. So now I can move my cereal box to the left, right, and you can see all these the messages in the console. So let me go ahead and stop the game. Okay? Um Let's head back now, let's work on the remaining three directions, so right, forward and backward. And what I'm going to do out here is as structure three else if blocks right after my and off L. F. So else F. Right, so I have the first else F. And then I have another one and then I have the last one for the remaining for the last direction. Okay, now out here, what we'll do is we want to check if the key code is actually the right arrow key. So what I'm going to do out here is pretty much what I did up here in line # nine. So I'm going to say, okay use the input class and then use get key down method, which is going to take as an argument key code and we're going to see if the right arrow key was clicked now, if the right arrow key is clicked, then we want to move the cereal box to the right. And let's just also put a debug some message here. So I'm gonna use debug dot log and moving right and then once again Online # 11, I'm going to do use this again. But remember this time we are moving along the positive X access so left is negative and then right will be positive. So all that's going to change here is basically it's not going to be minus anymore. It's just going to be plus. So transform transform dot position is equal to the current transformed position and plus Using the vector three structure. And this time it's moving along the positive X axis, it doesn't move in the y or z axis. So that's just going to be zero and zero. Okay? And we can try this out to control S to save. Come back mm and it tells us we have an error. Okay, So okay, we have an error because basically we can't leave these empty. So let's fill those out. It doesn't matter. So out here I'm going to check if the key code is going to be let's say a barrel. So once again input dot get key down and key code dot up arrow, right? And then we can have another debug dot log here which takes as an argument the string moving let's say we are going to move forward. Okay? So and then or we could say moving forward. Right? And then this is going to say um no, we're going to use transform dot position. Okay? And then once again and from that position this dot transformed dot position plus new vector three. And this is going to take as an argument zero F. So remember we're not moving along the X axis, we're not moving along the y axis, we are now moving along positive z axis. Okay, so the last one is going to be down arrow. So input dot get key down which takes as an argument, a key code. And then we're checking if the down arrow key has been pressed if so we can have another message debug dot log. And out here we can see moving backward. And then once again I'm just gonna copy this over here and this time if the cereal boxes moving back, it's actually moving along the negative z axis as a control is too safe. Okay and now let's check this out. So compiling scripts, no errors, which is good, clear. And let's see so moving left, moving right, moving back. Okay, so that is going to move back and that is forward. So we have the deep uh these log messages um kind of swapped. So what this should be is this should be moving backward, so and this will be moving oh forward. Okay and let's go back compile clear and when I move back and then I move forward, okay? So there you go, so I'm moving forward forward forward forward and it's out of the camera angle. Okay, So this is all for this cast. It actually forms the basis for the next cast, in which will actually have a character walking around in our scene, so it'll be an animated character that walks and is also in idle animation when nothing happens.
6 Views 0 Likes 0 Comments

Taking off from where we left off in the last cast let's get our cube moving to the left, right, backward and forward when left, right,up and down arrow keys are pressed

Comment
Leave a comment (supports markdown format)