Simple Player Movement in Unity

Christopher Pearl
4 min readJun 3, 2021

First thing first! We need to create an Gameobject in Unity, assign a script to the Gameobject, and assign a position to the object.

After I add an script to my new Player Gameobject, it’s time to jump into the script and write some pseudo code on how I found the current position of the Gameobject. Unity is a component based framework when it comes to coding. It works in a hierarchy structure, and I want to be able to access the transform of the Player, so within the Start method, I used the transform.position. Vector3 defines all position types in Unity and is used most times when the position of an object is being adjusted. I needed to use the “new” keyword as well so I could assign a new position for my Gameobject.

Moving Player

Next we need to get the player to move in one direction. I used the transform.translate, as this declaration moves a Gameobject in the direction and distance of translation. Using Vector3.right, this send my little Gameobject speeding off to right, into the distance, never to return! Yikes!

Moving Player at Normal Speed

The Update method is called once per frame, 60 frames per second. I want my player to move one meter per second. I want to convert one meter to one second, so I needed to use Time.deltaTime within transform.Translate code. I then included a multiple of 5 so that it would move 5 meters per second.

By creating a speed variable, I gained direct control of how fast or slow I want my variable to go! In this case, I made it a reasonable speed at 3.2f. When adding the speed variable, I replaced the multiple of 5.

Player Input

Next, I made my variable private so that it can only be accessed and referred to within this Player script. Also, I added a Serialized Field so the variable can be accessed within the Transform in case I want to adjust or test it, if necessary.

I wanted to make it so the user can control the Gameobject using a keyboard or controller and move the shooter around. Inside update, I used the horizontalInput and verticalInput variables which I checked within the Project Settings ->Input, in Unity, and matched. In this case, the A and D keys were mapped for horizontal movement, and the S and W keys were mapped for vertical movement.

Boundaries for Player

Vertical Boundaries for Shooter

I don’t want my player to move all over the screen and run into oncoming enemies, but rather have the coming toward it. So, I created a if-else statement and mapped the conditions for the y-axis. This restricts the gameobject from going too far up the screen and from exiting the screen down below.

Horizontal wrapping for shooter gameobject

Using if — else logic, I was able to create a wrapping affect for he player. Using a condition applied to the player position on the x-axis, the player will appear on the opposite side. This was applied for the player being on either side of the screen (negative or positive position on the x-axis).

These are just a few basics for movement in Unity. When creating a 3D game of virtual reality experience with depth, you can create many other ways to move an object.

--

--

Christopher Pearl

Unity Developer, AR/VR Developer, Virtual Reality Product Manager for Micro Medical Devices Inc.