Basic 2D Movement in Unity

Brian Ratnasinghe
3 min readJun 25, 2021

Hi everyone, this article is going to be going over some very basic 2D movement I accomplished in Unity. This article is intended to serve as the framework for a bullet hell type shooter game, so stick around if you want to final results.

To start of I simply set the camera’s transform component to be 10 units away from the origin, and I created a script component to spawn the player at the origin.

After setting the skybox to be black here is what the game view actually looks like.

Next I began coding the ability of the player object to respond to user input using Unity’s built in update function. The Start function is only called on the first frame the game is run. Alternatively, the Update function is called on ever frame.

To start off I wrote a few lines to receive the user’s horizontal and vertical input. The values for the user input are stored in the variable called horizontalInput and verticalInput.

Then, in order to apply this input to the transform component of player object I multiplied the user input to a Vector3 while calling the transform component’s translate function.

This code works as intended, but resulted in one small issue. Even though the player was able to move there was nothing stopping the player from moving offscreen infinitely.

There are two different approaches to fix this issue and I actually applied both of them. To prevent the player from moving indefinitely on the y-axis I made it so that when its y value passed of 3.8 or 0 it would be reset back to those values. Next, when the player is player is travelling horizontally and passes an x value of either 11.3 or -11.3 the transform component on the player would be teleported to the other side of the screen.

That it, I have now complete the basic set up for player movement in a game. Thanks for reading.

--

--

Brian Ratnasinghe

Here I am documenting everything I am learning in order to become a successful game developer.