Game Development with Unity for Web Developers Movement Component Script
Transcript from the "Movement Component Script" Lesson
>> I've talked a lot about this one scripts. Let's actually go write it. So I'm gonna switch back into Unity here. And there's a couple of different ways that you can create scripts in Unity. For example, if I have this player game objects selected, oops. And then I scroll down here to Add Component.
[00:00:29] I could type in this script, we called it rolling movement. And there is no rolling movement script found so we could hit new script there. If I do hit new scripts It's going to create that in the assets folder. So we could create it. It would get created in the assets folder and get attached to the player game object, and then we'd have to move the script to where we want it.
[00:00:58] The way that I prefer to work is I like to create scripts inside of my project window. So I'm going to go to the Scripts folder here. And if I right click in any of this blank space, I can go to the Create Menu and I can choose C-Sharp script.
[00:01:21] So, again, I want to call this script rolling movement. And when I hit Enter, it's going to create the script. And it's going to reload the domain that's sort of the magical compilation process that allows us to just hit the play button. And in the inspector, you'll see a preview of the script and it's actually added a lot of stuff in there by default, some of it is useful and we'll keep some of it.
[00:01:56] Some of it is stuff we don't want. So now I need to go to the player game objects. And we need to add that script as a component, so I can do that a couple of different ways I can click and drag this rolling movement script. I could drag it to the player in the hierarchy, I could drag it onto the sphere in the The scene view or I can drag it over to the inspector.
[00:02:26] So if I drag and drop over to the inspector, it will add this rolling movement script as a component. It's not going to do anything right now because there's nothing in the script. But if I double click on the script from the project window or you could also click on it from.
[00:02:46] From the inspector, it will open up in whatever default editor you have chosen, in this case I'm using VS code. Many installations of Unity will have visual studio installed, and there really isn't a right or wrong answer here, you can use pretty much any IDE that you want.
>> Is there a good IntelliSense support or an extension for Unity for VS code?
>> Yes. And I believe that comes installed with the Unity If I might have to get back to that one and dig into that a little further, but yeah, unities installation processes pretty good.
[00:03:33] I think a lot of that stuff often comes installed already.
>> Is it better to use fixed update instead of multiplying by delta time and the update method?
>> I mean you can multiply by delta time to get consistent, while somewhat consistent updates but they are going to be frame rate independent.
[00:03:57] And the other thing that's gonna happen is those physics calculations are going to happen every time a frame is rendered. Which could potentially impact performance especially for doing very complicated physics calculations. So keeping them in fixed update, even though multiplying by delta time and update might allow you to keep it smooth and consistent.
[00:04:20] Keeping it in fixed update allows you to keep the CPU load consistent. There any other questions? No all right cool. So I've opened this rolling movement script It comes with a couple of things we need ,a couple things we don't need. You'll notice up at the top, there's a few additional namespaces System.Collection part of or, excuse me, System.Collections.
[00:04:52] And System.Collections.Generic are both part of C# And .net and we don't actually need those. We just need the Unity engine name space, because we are not going to use any collections in this game. That's things like rays, lists and other C-Sharp niceties that allow you to work with things like linked lists, but we don't need those.
[00:05:24] So we just need the Unity engine, namespace leaving the other namespaces that you're not using as harmless they'll get stripped but for the sake of simplicity, we'll remove those. We do need the start method. The update method, we actually wanna change to Fixed Update. And again That's pretty important here.
[00:05:46] We don't wanna update every frame, we wanna update every fixed physics update. And then there's also a couple of comments that a Unity script includes. I like to just delete those so that I can write my own comments. So the first thing that we're gonna do inside of this class is we're going to write out those variables.
[00:06:12] And the first one is the speed variable and it has an attribute of serialized field and Then that is a float. I call it m_speed and I set the value to one with an F there because it's a floating point value, and we're gonna end that line with a semicolon.
[00:06:39] Next we need to keep track of the rigid body component that is on this same game object to which our rolling movement component is attached. So we're going to create a variable just to hold on to that rigid body. We need to add a hide and inspector attribute to this public vector three and we're going to call this movement Direction.
[00:07:14] And we're actually gonna write another component that's gonna derive that vector three later on, and that's why we need it to be public. We need it to be accessible by other scripts. And then finally, we're going to add a vector three which is going to be called m_startPosition.
[00:07:39] And just really briefly, a vector3 is a three dimensional vector with an x, y, and z. And that just stores internally a struct with three floating point values. And you know a vector does typically have a position and a magnitude. In this case, a vector three can represent one or the other.
[00:08:04] So it can represent a position or a magnitude. In this case we are going to use that as kind of both. We're gonna use that as well. We're gonna use it as a direction in this case, and then we'll multiply that whole vector by this speed variable. So in our start method, the first thing we need to do and I'll just write a comment here by adding two slashes.
[00:08:35] Is we need to gather components, variables that will be needed later, and what are those well we have a variable for our rigid body. But we need to actually grab that rigid body off of the game object that this same component is attached to. So we'll use the rigid body variable.
[00:09:02] And we're going to assign it using this special method here called get component. And get component takes a generic type. So in these angle brackets here, I'm going to type rigid body okay? And then right after the angle brackets, I'll type an opening and closing parenthesis because it is a method It's a GetComponent method.
[00:09:32] But we're using rigid body as a generic type to assign it to our rigid body variable okay? And then finally, we need the starting position here. So we'll use the start position vector three. And that's going to represent a position versus To say this vector three which is a direction right and there's a couple of components that are accessible without having to use that get compass.
[00:10:05] Component method and one of them is transformed because every game object has a transform. This is a mono behavior, it's a component that's going to be attached to a game object. So we know that there's gonna be a transform. So we want lowercase transform with a lowercase t here Or rather than uppercase.
[00:10:26] Uppercase is going to be the actual transform class where we might access static methods without a transform that's instantiated. We want the transform that's attached to this game object. So we're going to keep it lowercase. So we're gonna say transform.position, which is one of its properties. Because remember in the inspector, we saw a transform has a position, rotation, and scale.
[00:10:53] So we want its position. The reason we're storing the start position here is because we're gonna need that when we reset. So you remember how the spheres were kind of falling off the side. It or when we start the game or when the game ends and you want to restart the game we need that original position to reset the sphere so that's why we're storing it and start because we'll use the steam start.
[00:11:22] Position later before the game has actually begun. So, oops, there we go. okay, next, inside of fixed update, we just need to do one thing here for now. We need to apply that movement direction as a force To the rigid body and I'm going to type that out as a comment.
[00:11:46] So we remember what we're doing here. So every physics tick or physics update, out of force equal to the movement vector multiplied by speed good okay. So, on the rigid body class, there's a method called add force okay? And add force along with many methods in the Unity API is overloaded methods.
[00:12:23] So if I type add force the parentheses here. It will show me in VS code or Visual Studio, the several different overloads that I could potentially apply there, right? But I actually want the second one. And you don't need to scroll through these to actually select which overloaded method you want.
[00:12:50] You can just type in variables, it's going to know which one that you mean. But I want the second one here I want to add a force as a vector to the rigid body, so I'm pointing in a direction Of this vector, it's gonna push it in that direction.
[00:13:09] So we want to use our movementDirection, And then we want to multiply that vector, by our float value of speed. Because we have a direction that direction is going to be coming from. In the case of the player, the keyboard when I'm hitting the WASD keys that's going to be moving that vector around.
[00:13:36] And then the speed gives that a magnitude. So it increases the length of that factor. So the last method that we're gonna write here is going to be called reset position. And this is going to be a public method. I left off accessors here because these methods can be private by default, but we actually need a public method.
[00:14:00] For resetting the position here, because this is one of several methods that's going to be called when you say hit the spacebar to reset the game or when a player rolls off the side we need to reset its position. So we're gonna do a couple things in here, but I'll type out a comment to indicate kind of the general gist of what that is.
[00:14:25] We're gonna reset the velocity to 0 and set the position back to start. So I'm gonna type this out first and then explain it. So first, on the rigid body We are going to set velocity = vector3.zero, so. the vector three class has a stat Attic property called zero and that allows you to just assign a vector with a value of zero for all of its values.
[00:15:04] So we're set. So a rigid body internally is storing a velocity. It's how fast the rigid body is moving. We wanna set that to zero so it's not moving then A rigidbody dot angular velocity, so it's not only moving it's also potentially tumbling through space. We want to set that to vector 3.0 as well.
[00:15:27] We don't want any angular velocity. And then finally, the transform dot position Now we want to set to the start position. So you'll kind of notice that we've mirrored what we've done in start. And start we're taking the start position vector three variable and assigning the transforms current position at the start of the script when it first starts rendering.
[00:15:56] Down here we're doing the opposite. We want to assign that start position that we stored to the transform circles back to its original position. So that's the kind of the naive approach right? You reset the start position. When people are starting out in Unity, they'll do this type of thing where they have a physics game, they'll reset a position.
[00:16:22] And what ends up happening is they'll reset a position but then the rigid body will continue moving. And that's because it still has the same velocity as it did when it was in another position in space. Is it's kind of like in the game portal. When you go through a portal, you maintain that velocity, they kind of use that to their advantage in that game, right?
[00:16:41] In this case, we don't want that when, when the sphere start above the board, we want them to have velocity and angular velocity of zero so that they just use gravity and drop in right? So that's why we're resetting the rigid body there. okay, so we wrote the script, I'm going to hit Command S to save it.
[00:17:05] And when I switch back to Unity, you should see a very brief little loading bar again. It's magically recompiling our game on the fly. With it selected you'll see in the inspector all the stuff we just wrote. And the game should at this point if we hit play the player should drop in.
[00:17:32] And if we hit the input keys, it will do nothing. Let's because we aren't actually doing anything with that movement vector just yet. We need to write another script that's going to drive that vector. But I can hit play again to come out of play mode. And if I hit play once more You'll see that rigid body does have gravity it's dropping in but there's still more that we need to do