Day 5: Simple Player Movement in Unity!
Now we have a little understanding of all the windows in Unity, it’s time to start coding! The fun part! Today We’ll talk a little about simple player movement.
To start off in the hierarchy right click and go to 3D object and add a Cube. Once the cube is created go to the transform component and click the little gear to reset the transform.
Now that’s done it coding time! As you can tell the transform is what position’s the game objects in Unity. We can use the transform to move the player as we would like. If you search “transform movement unity” in google, you may find transform.Translate();.
This is how we will move the player. Now to put it into a script! In your project window right click, create, and click folder, name it scripts. In this folder, right click create, click C# Script. Name it player. After naming the script open it.
Before opening make sure you add the script to the player object. Once it opens, you’ll see a lot that’s going on. Today we wont talk about every thing but just to get the player moving and tomorrow we’ll discuss more about what we see in a script. We will take a sec and talk about the Start() and Update() functions. The Start() runs when the game first starts up. The Update() function will run every frame, which is about 60fps. This will depend on how well the computer runs.
When the game start’s, we would like the player to start a certain position. To do that we will use the transform class but not translate. We will use the position of the transform. It’s good to know that the position of 3D game objects use Vector3. Vector3 runs on X, Y, and Z coordinates. To set the position we will need all three coordinates. To set the position to zero we will use this line of code, transform.position = new Vector3(0,0,0);. The new Vector3 is creating a new vector at those coordinates.
Now for movement. In the Update() we will add player movement. We will add transform.Translate(), you can see on the tool tip that the Translate needs a vector 3. We will add Vector3.right, this is like putting “new Vector3(1,0,0). We will need to multiple this by “Time.deltatime;”. Time.deltatime is the time it took to process each frame. This allows for slower computers to run have the same reaction time in the game.
When you run the code you’ll notice the player will zoom off in the right direction. Like it’s traveling at the speed of light!
To fix this we will add our first variable. This will allow up to control how fast the object will move. Above the Start() function and below the Class name add public float speed = 3.5f;. After you declare the variable, go back to transform.Translate() and multiply it by the vector3.right.
Now when you run the game, the object will be slower! When we declared the speed variable, we put public in front of it. This allows us to control the speed in the inspector as well! We got the player to move on it’s own! Tomorrow we will discuss Player input to move the player!!