How to Move a Player Character in Unity - Unity Programming Basics

How to Move a Player Character in Unity - Unity Programming Basics

So I am creating a Shoot em up, a classic genre which I loved back in the 8 bit and 16 bit era of gaming. I boot up Unity and import some game assets, place a background and my player character on screen...but now what? How can I get my character to move?

The answer is through scripting and if you google this topic, there are a multitude of ways in order to do this. Here, I will share my simple method. I've created a script folder in my Project panel and inside have created a C# script simply entitled - "Player.cs". I have then dragged and dropped that script to my player game object. In this case, a rather fancy looking spaceship.

So the first thing I do in the script, is set a Speed variable. This is important as we need a value to tell unity how fast to move the ship in a given direction. I have also made this private variable accessible to the Unity editor by appending the Serialize Field attribute to it. This means that we can change the speed property "on the fly" as the game is running in the editor. Really useful if you want to experiment with values and try to obtain the optimum conditions for the game. For instance in this case, we don't want the spaceship to move to fast or be too slow.

In this case I set the initial value to 5, however if on play testing the game we find that value is a little too fast, we can change it in the editor and experiment with the speed.

Next I create a separate method or function for the movement logic. I try and keep my classes and game design fairly modular and keep the Update function for specific game logic. Within this function, I check the horizontal and vertical axis for specific values. I want to limit the player movement so that they cannot move the whole way up the screen and I want the game to check if they have moved off the left or right horizontal axis and 'wrap around' if this is the case.

So in the above code, you should see that I check the player input for both horizontal and vertical axis and apply a vector for right or up depending on the input. You should also see how the speed variable plays a part in this code. The Time.deltaTime limits the frame rate of the game so if a player is playing on a fast PC the speed of the character is not super fast.

Below is the implementation of the game logic in action!


To view or add a comment, sign in

More articles by Dr Chris McAuley

  • Creating Spawn Points in Unity

    Every Multiplayer game requires spawn points for the players to be generated into the game environment. This is an…

  • Creating a Network LAN Game in Unity

    Single player games are fun but eventually gamers will expect some multiplayer functionality within your gaming…

    1 Comment
  • Travelling to a Goal Location Using Vectors

    In this article I want to look at how Vectors can help in making an NPC character move towards a basic goal. The object…

  • Vector Mathematics in Gaming

    One of the most important concepts to understand in game development is that of Vector Mathematics. A 3D character has…

  • Beginning a Journey in Comic Book Art - Exploring Flatting

    I recently started an adventure in learning how comic books were put together. Working with world famous colourist John…

  • 1hr Game Coding Challenge : Duck Hunt

    Recently I became involved in a series of game development challenges. The latest was coding a retro-inspired title in…

  • What is Gamification and How can it help my Business?

    Since earning my accreditation with The Engagement Alliance I have been asked several times by co-workers and friends…

  • Shooting a Laser - Instantiating an Object - Unity Basics

    In my previous article we looked at the basics of Player movement in Unity. This is another 'basic' but fundamental…

  • Cryptography with Chris #1 The Caesar Cipher

    I've been studying a digital cryptography course on Udemy recently. It's pretty good and gives an excellent basis for…

  • Using Mixamo to Create Character Animation

    Mixamo is an online tool which aids animators in creating rigged animation for custom 3D characters. It's a simple and…

    2 Comments

Explore content categories