Movement Of a 2D Player in Unity.

Movement Of a 2D Player in Unity.

What is Unity?

Image for postUnity

I started this from the worst way that someone can start a document. Everyone knows Unity 3D is a Game Development platform.

Unity is a cross-platform game engine developed by Unity Technologies, first announced and released in June 2005 at Apple Inc.?s Worldwide Developers Conference as a Mac OS X-exclusive game engine. As of 2018, the engine had been extended to support more than 25 platforms. The engine can be used to create three-dimensional, two-dimensional, virtual reality, and augmented reality games, as well as simulations and other experiences. The engine has been adopted by industries outside video gaming, such as film, automotive, architecture, engineering and construction.

Several major versions of Unity have been released since its launch. The latest stable version, 2019.3.10, was released in April 2020 as Wiki says.

Image for postUnity Home Page

Anyone can Download unity by visiting this URL and have the newest version of it.

After Fundamentals,

To see the way how a 2D player movement scripting must be done, We have to start a new 2D project.

Image for postMy Unity Platform

After starting a new project, (Unity will get some time to load) then we have to import our 2D model to the Unity screen. This time I am using a default Sprite provided by unity. We can do this by right clicking on the Hierarchy window and selecting new 2d object.

Image for postSelecting a sprite.

Then it will be on our Scene layout and I did some adjustments like changing the size and color, Changing the Sprite shape to a Cube, like that..

Image for postMy Changes

Then We have to add 2 main components to make sure everything works fine after. Those two are Rigidbody 2D and Box Collider 2D. On the inspector window/panel there is a button called Add Components. Click on that and we can select those two attributes to our main Character(square)

What is Rigidbody 2D ?

A Rigidbody 2D component places an object under the control of the physics engine. Many concepts familiar from the standard Rigidbody component carry over to Rigidbody 2D; the differences are that in 2D, objects can only move in the XY plane and can only rotate on an axis perpendicular to that plane. Adding a Rigidbody 2D allows a sprite to move in a physically convincing way by applying forces from the scripting API. When the appropriate collider component is also attached to the sprite Game Object, it is affected by collisions with other moving Game Objects. Using physics simplifies many common game play mechanics and allows for realistic behavior with minimal coding.

Rigidbody 2D

Switch to Scripting A Rigidbody 2D component places an object under the control of the . Many concepts familiar from?

What is Box Collider 2D ?

The Box Collider ? An invisible shape that is used to handle physical collisions for an object. A collider doesn?t need to be exactly the same shape as the object?s mesh a rough approximation is often more efficient and indistinguishable in game play. There are lot of properties, Inherit members, public and static methods inside this.

What to do next ?

First of all set the gravity scale to 0 on RigidBody 2d. We don’t want to float out cube/square like the space 🙂

Image for postOur Rigidbody

If you want you can freeze the rotation and keep the cube steady without rotating when collide with something. Just have to tick the Freeze Rotation on the Constrains on RigidBody 2D.

Then We go Scripting.

We can add a new script by clicking on the Add component button and searching for a script. I am naming this as Player Controls.

Image for postAdded Script

After creating the script we have to code for the movements and the code will be like this.

Image for postMovement Script

We do not need any start method for this. just need the update method.

Vector2 :- It is representation of 2D vectors and points, used to represent 2D positions, only two axis x&y.

Vector3 :- It is representation of 3D vectors and points, used to represent 3D positions,considering x,y & z axis.

Since the project is 2D, we just have to create variables for X and Y axis. First I created the speed variable as a Vector2 (you know why) and assign 50 as value. It is a public variable and X/Y will be displayed on the Inspector section and can be customized.

Image for postInput for X/Y

What happened here was I created 2 variables and assigned them to the two axis (X/Y). Words like Horizontal and Vertical must be spelled as same as above because they are per-assigned in unity.

Image for postEdit -> Project Settings -> Input

Then we can simply use the arrow or w/a/s/d keys to preform actions with our cube. We have to multiply them by the speed variable.

Image for postAll done

I created a Vector3 variable called movement and it include the multiplication of the Axis. You can see a 0 here, because there is no Z axis in our little cube. 🙂

Image for postTime.deltaTime?

After that the movement variable is multiplied and assigned again with a variable called Time.deltaTime. The completion time in seconds since the last frame (Read Only). This property provides the time between the current and previous frame. Use Time.deltaTime to move a GameObject in the y direction, at n units per second. Multiply n by Time.deltaTime and add to the y component. MonoBehaviour.FixedUpdate uses fixedDeltaTime instead of deltaTime. Do not rely on Time.deltaTime inside MonoBehaviour.OnGUI. Unity can call OnGUI multiple times per frame. The application use the same deltaTime value per call.


using System.Collections; using System.Collections.Generic; using UnityEngine; // Time.deltaTime example. // // Wait?

Then we have to pass the main movement variable into transform.translate( ) function. Moves the transform in the direction and distance of translation. If relativeTois left out or set to Space.Self the movement is applied relative to the transform’s local axes. (the x, y and z axes shown when selecting the object inside the Scene View.) If relativeTo is Space.World the movement is applied relative to the world coordinate system.


Suggest a change Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all?

So, That is it.. Yeah we have a moving cube now.

Image for post

I uploaded the code to GitHub and anyone interested or needed can grab it.


Contribute to save-snowden/Movement-Medium-Artical development by creating an account on GitHub.

Peace !


No Responses

Write a response