Unity — Creating player movement Day 2

Benjamin Calvin
4 min readSep 9, 2021

--

Objective: Allow the player to move the cube using the awsd keys utilizing a 3d environment as 2d.

In the previous article, we set up our game scene and ensured we understood the basic creation of a game object and script.

UPDATE:

After further discovery, it’s completely unnecessary to call separate transform.Translate with random direction in the horizontal or vertical.

From:

transform.Translate(Vector3.right * horizontalInput * _speed * Time.deltaTime);transform.Translate(Vector3.up * verticalInput * _speed * Time.deltaTime);

To:

Vector3 direction = new Vector3(horizontalInput, verticalInput,0);transform.Translate(direction * _speed *Time.deltaTime);

Now onto the original walkthrough

transform.translate & Vector3

Vector3

This structure is used throughout Unity to pass 3D positions and directions around.

Transform.Translate

Moves the transform in the direction and distance of translation.

This is what we will use to create movement in the game. Going into the Player file, we added to Update():

transform.Translate(Vector3.right); 

The script is already attached to the player, let's see what happens when we press play.

It went to the right as fast as 60 frames per second. Lets fix this by multiplying Time.deltaTime to the Vector3. Then lets play to see what happens.

The cube now moves much slower because it's using the Time class to go slower than 60 FPS. Two problems still arise. The player goes off the screen and never comes back. The second issue is the player is not receiving input from the keyboard it is moving on its own.

Going into Unity and going to edit > project settings > Input Manager > Axes(Plural for Axis). There are two categories, Horizontal and Vertical. Each has what the current project has assigned the key to. With this in mind we can use

Input.GetAxis(“Horizontal”);

This syntax can be applied to both vertical (up and down)

As well as Horizontal(left and right)

After we dedicate these inputs to variables, we can add them to transform.Translate that control the direction. Mind you the use of specifically right or specifically up with Vector3 does not matter, it will translate to whatever key your are pressing.

Also to ensure our player does not go at the speed of a turtle, I have added a speed global variable to assist in going faster. The result is:

This is great! We now have a moving player that moves with the keyboard. Our next problem is the player can go off the screen, this is where the if statements come in. But first, we need to figure out where our X and Y borders are.

Within the scene, view utilizing the move tool to move the player object on the x and y-axis right after they disappear from the screen for X, so we can have them appear on the other side. The Y-axis we are sticking to an on-screen border

After we figure out where we want our border, we utilize if/if else statements like below.

  • The first if statement limits the uppermost portion of the scene while pulling the current x position.
  • The second statement limits the very bottom of the scene while pulling the current x position
  • The third statement not only limits the right position, pulls the current Y position, but also moves the player to the other side of the scene to give that seamless play feel.
  • The fourth statement limits the left position, pulls the current Y position, and moves the player to the opposite side of the screen.

After getting this far, all these statements are in the update method. Let's put all the code from Update() in one method called PlayerMovement and call that function in Update().

Now you have basic movement, some troubleshooting skills, and the ability to limit the scene. Until next time…

--

--

Benjamin Calvin

Dedicated and motivated individual learning programming and sharing my discoveries with you.