Move your little game player to feel the Magic

Samarth Dhroov
4 min readMay 1, 2021

This article is going to help you make that first move into the world of game development. So, stay with me for a few more minutes :)

Before we get into anything related to Unity, we must address the concept of Vector & get it right.

So, Instead of writing elaborately, I’d like to take the help of Unity’s own tutorial that you shall see without fail to understand the math.

Please don't worry :) It's not that hard.

Unity

Now that you have seen it, you know that the whole point is to move from one point to another and when it’s done at a certain speed, we see it as a movement. It is a computer trick :)

Please keep this statement in mind while working with Unity.

The architecture of Unity works in a hierarchical structure and hence you’d have to go through a step-by-step process before you get any requested item.

As you can see here, the 3D cube is set at a vector position (0,0,0). Our aim is to get input from the keyboard and control where this position shifts to.

Unity

So, let's begin with the first line of code in the visual studio after creating a C# script in Unity and attaching it to the player cube.

Unity

Here, you can see two default methods.

  • In the start method, we shall give the starting position where we would like our player to be when the game begins.
  • Since the position data seats under the transform component, we have to go knock on its’ door first and then ask for the details.
  • Write the code as shown below, save the script and run the game. You shall see the cube beginning right at the bottom center part when you click play.
Unity
Unity
  • Now, shift your focus to the update method that runs 60 frames per second. Yes, so if you write a piece of code in this method, your computer will run it 60 times a second. Cool han!
  • Here comes the vector :) We’d like the cube to shift right-left and up-down which means x or -x, y or -y. The z part can relax for now.
  • The movement is taken care of through a pre-defined method called translate from the class transform.
  • The question is how would you get input from the keyboard for knowing the direction. Here comes the input manager of Unity.
Unity
  • The tool here has already done all work for you. All you have to do is use it by utilizing GetAxis( ) method of the Input class and pass the name of axes whose data you are looking for. Simple!
C#
  • Also, keep in mind that the data we are getting is oscillating between -1 to +1 contrary to a boolean response that we get in buttons.
  • So, we would have to combine the data here with the vector. We do it by using vector3.right and vector3.up which is a concise version of vector3(1,0,0) and vector3(0,1,0).
  • However, as said before, the update method is going to run 60 times a second. If you go ahead with it, your player will be “flash” of the justice league.
Justice League
  • Therefore, we would have to use two other supplements to make the movement smooth.
  • First, instead of running the engine for 60 frames a second, run it at 60 meters a second.
  • Second, we get an accelerator in our hands by declaring a speed variable.
  • The final code shall look like this at the end. (I have given 10 m/s speed.)
C#
Unity

There you see your player moving and I know how it feels :)

There is a lot more you could do here but for the first step, this is good.

Thank you very much

--

--