It’s Alive! Basic Player Movement in Unity.

Gabriel Perez
4 min readMar 24, 2021
It’s Allliiiiive! Mwhahahaha. Day 8

Hello everyone! Today I started implementing basic movement to the Player.

I initialized and declared a couple of local variables of type float in the Update() method to store the input of the player movement.

The Update() method derives from Unity’s MonoBehaviour. It is considered a game loop and it is called once per frame, 60 frames per second.

I then created a local Vector3 that will use the local float variables of the input. Vectors are used to manipulate the position of the game object. A topic I would like to write about sometime. In this case, I want to use a new Vector3 using the input system from Unity.

I store a new Vector3 using the input local variables as part of its arguments.

We then have to use the movement variable where the new Vector3 is stored using the input variables as part of the argument of the method.

IMPORTANT TO NOTE: Every game object has a permanent Transform component attached to it.

The Transform component is attached to the Player game object.

For me to grab the player's position, I have to grab its transform component.

Transform is special because it’s on every game object created in Unity, but also it’s responsible for the manipulation of the game object's position in 3d space.

REMEMBER: every game object has a permanent Transform Component

To further engrain this truth, every game object has a permanent Transform component attached to it. I hope this will stick from now on!

I know that the Player script is attached to the Player game object. This allows me to grab the transform of the Player object.

We can access the player’s transform component by typing “transform.” The script must be attached to the Player game object.

By typing transform, I am saying that I want to grab the transform from the object this script is attached to. I can go further into the transform by typing a period. This way we can access the Translate function from the transform component. We want the player to translate its position with the movement vector we created above.

For the player to have a set speed, we must create a speed variable of type float and times it with the movement vector and Unity’s Time.deltatime. This will allow us to always move at the speed we want across every frame.

Also, I want to mention what normalize mean after the period in movement. Normalize will make sure all movements in any direction have a magnitude of 1. This way certain directions won’t double our speed when pressing more than one key. Especially the angle movements.

I like to clean my code when necessary. The Player movement code is short and placed in the Update() method for it to constantly loop. It’s readable and it doesn’t really need to be refactored.

Visual Studio Tip: Highlight selected code, hold CTRL, and press the period key on your keyboard for available extraction options.

But the ADD in me tells me to encapsulate the movement code into a new method so that the Update() method can call it. So I highlighted the code in the Update() method, I held the CTRL key and pressed the period key on my keyboard using Visual Studio. A prompt popped up, and I selected the Extract Method option.

This allows me to easily create Methods on the fly by highlighting existing code. This also works with variables. You can create a non-existent variable, highlight it, and extract it.

Here’s how my Update() method looks like now. Simple and clean. Sure, I can even break this down further if I wanted to, but the code is simple that it doesn’t need to be refactored further.

The PlayerMovement() method called in the Update().

After implementing a simple system, I always keep in mind to commit any changes through Git Bash. This way I can practice how to pull, commit and push any changes through the use of branching off the main and merging it with the dev branch.

Git is Love. Love is Git. I am falling in love with version control!

That's it for now. Thank you for reading!

--

--

Gabriel Perez

Hello everyone, My name is Gabriel Perez, I am a Unity Developer and a creator who is always learning and experimenting.