Making a simple player movement in Unity

Objective: Make the character move in the x and y axis.

In this article you’ll learn how to make a simple character movement using the old Input System.

First, let’s create a C# script called Player.cs clicking on the project windown then create > c# script

Note: I recommend to take a look at the preview window on the inspector, be sure that the Class’s name is the same as the Script’s name. If not, then you’ll not be able to add the script to the GameObject.

Let’s Code!

Once you open your script by clicking it, your preferred code editor will be shown.

Note: Most common code editors for Unity are: Vs Code, Vs Community and Rider.

Right now you should be looking at something similar to the image above.

Basically, the Start method is that piece of code that will be executed once, when the game just started. And the Update method is executed every grame.

We will be using the Update method for this guide since we want the character to move constantly.

Declaring the Speed variable

The reason why we put the [SerializeField] attribute it’s because we want to see that variable in the inspector to be modify.

Let’s get back to our code and create the Movement method.

This is how it should look.

We first declare two variables: x and y that will be taking the player’s horizontal and vertical input when the respective keys are pressed.

Note: In my case AD are for horizontal and WS for vertical axis.

Let’s test the Script

Place a cube in the scene. And add the Player.cs script to it.

And our character is moving!!!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response