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!!!

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

MIGRATING LEGACY ENTERPRISE SOFTWARE TO MICROSERVICES

Basics of Unit Testing in Angular

13 Problem Areas in Data Infrastructure to build NewCos

Learn Git & GitHub in Under 6 Minutes

RISE Newsletter — July 2018

Knowledge and Competency Importance in Studying Engineering

This Programming Language ‘Feature’ Drives Me Crazy

Team Collaboration with GitHub

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Daniel Ercilio Del Rosario Guerra

Daniel Ercilio Del Rosario Guerra

Loading...

More from Medium

My journey becoming a Unity game developer: 2.5D Infinite Runner-Prototyping

Bug Fix on Model Flip

Using the Unity Animation System (Game Dev Day 59)

Hidden Shortcuts to Make Level Design Easier in Unity.