Simple Player Movement in Unity

Objective: Create a simple player movement for our Space Shooter Game.

Player Movement

The most fundamental aspect of nearly all games is being able to move the player around the game scene. You may be asking yourself “how do they do that”? You're in luck because I will be showing you a basic movement using a script and then tie that to user input.

The first thing we need to do is figure out what controls the movement of the player. you will notice below when the player(cube) is moving the X and Y position in the Transform are changing.

Player Transform

Now that we know the movement of the player(cube) is controlled by the Transform of the game object we will need to research how to use that in a script. Unity provides an easy way to access the documentation and that is the “question mark” icon. You will find it at the top right of each component. When you click on the icon you will be taken to the documentation on that component, in this case, the Transform.

Searching the Transform Scripting API

Above you can see me searching the Transform documentation. We are looking for information on scripts so you will notice I clicked on the link for scripting. In the scripting documentation, I looked for a method that would work for what we needed, in this case, it is moving the transform. I found the method “Translate”. The syntax is below

Translate Syntax
Translate documentation

Now that we have the method we need to move our player we are ready to create the script that will execute the method and add it to the player.

Creating the Player script

In the update section of the script, we will add our code for moving the player.

Update code
Player moving fast

One thing you will notice is the player(cube) flys off the screen. After some research, you will find that you can use time. delta time to move in realtime

Time.deltaTime
using Time.deltaTime
Player moving slower

Now that we have the player moving in real-time, we want to tie that to user input. First, we need to find where the user input is stored. Click on the edit menu then project settings then input manager. There you will find the horizontal and vertical axes.

Input Manager

To use these axes from the input manager, we will need to create a variable to store the information.

User inputs

Next, we will add the variables to our transform. Translate statement.

Translate using user inputs

Now you should be able to get things moving in your game.

Happy Coding!

--

--

--

Unity / C# Game developer

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

Recommended from Medium

2017 Trends in Web Hosting

Front-End Web Developer Roadmap

AWS Well-Architected Framework: 6 pillars of successful architectures

Why and How I made DevXplore(search engine)?

DevXplore -Search engine

Use case of JavaScript

Experimentation helps retailers optimize their supply chain — here’s how

Python probability game puzzle for data scientists

Android System Services AOSP

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
Bill Rislov

Bill Rislov

Unity / C# Game developer

More from Medium

Create an ability system using Scriptable Objects

Upgrading Controls on a Forklift to the new Unity Input System

Day 111: Introduction to Tilemap in Unity

Dust to Dust — Creating and Destroying Objects in Unity