A Very Simple Movement Script In Unity

A good way to explain the basics of Unity scripting is to make a very simple script that simply takes the A and D or left and right arrow keys inputs and moves the gameobject with those values. Let’s take a look at the following example.

using UnityEngine;
using System.Collections;

namespace PlatformerExample.Scripts {

   public class SimpleMovementController : MonoBehaviour {

      private Rigidbody2D m_Rigidbody;
      private float horizontalInput;
      [SerializeField] private float movementSpeedMultiplier = 20f;

      // Use this for initialization
      void Start ()
         m_Rigidbody = GetComponent<Rigidbody2D>();

      // Update is called once per frame
      void Update ()
         // Collect inputs here
         horizontalInput = Input.GetAxis("Horizontal");

      // Do physics related operations here
      void FixedUpdate()
         // Now the input values are used for altering the velocity of the rigidbody
         m_Rigidbody.velocity = new Vector3(
            horizontalInput * movementSpeedMultiplier,

At the top of the file, we import our libraries. Most important of these is the UnityEngine -library. Without it, we don’t have access to Unity Engine’s various classes.

Next we have the line with RequireComponent. This is used to prevent situations in which there isn’t a RigidBody 2D -component attached to the gameobject. The Unity editor prevents this script from being attached to a gameobject without a RigidBody 2D -component.

In the Start() -method we assign a reference of the RigidBody 2D -component that is attached to the gameobject to the m_RigidBody variable.

In Update() -method, which is run every frame, we read the inputs. In this case, we want the horizontal inputs, in other words the inputs from whatever controls are bound to the horizontal axis. By default, these are A, D, left arrow and right arrow keys on the keyboard. We store the value of this axis to horizontalInput. The limits for this value are -1 and 1, so the values are always something from between. When left arrow is pressed down, the value is -1, and for right arrow it is 1.

We then apply this value to the velocity of the RigidBody in the FixedUpdate() -method. Unlike the Update() -method, FixedUpdate() isn’t dependent on the framerate, but rather operates based on real world time, which makes it ideal for physics related tasks. In the script, a new Vector3 is assigned on every loop to RigidBody.velocity. The Vector3 consists of 3 values: X, Y and Z. As this script is for 2D, we ignore the Z -axis, and assign a 0 to it. The X value is the horizontal input multiplied by the movement speed multiplier, which gives the movement the necessary speed increase. The Y value is the current Y-axis velocity. If that is not assigned to Y value, it causes a “floaty” effect. That is due to the effect of gravity being overwritten. So to retain the effect of gravity, we assign the current Y-axis velocity to the new Y-axis velocity.

That is the script “in a nutshell”.

You can download all the code and a sample level from here.
All the amazing art that is in the package is done by Kenney. You can download his Platformer Asset Pack Redux from here.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s