How to control a rectangular cube using arrow keys in Unity

User input control is fundamental in game development and interactive applications. Arrow keys, present on most keyboards, offer intuitive navigation and interaction with objects in 2D or 3D environments. This answer will guide you in setting up a Unity project, creating a cube object, and writing a C# script for arrow key control. Upon completion, your cube will respond smoothly to user arrow-key presses.

Setting up the Project

  1. Opening Unity: First, we create a new Unity 3D project or open an existing one. The default project includes a “Main Camera” and a “Directional Light” in the “Hierarchy” window.

  2. Creating a cube GameObject: Unity provides primitive shapes like cubes, planes, etc. For this project, we will use a cube GameObject. We’ll right-click on the “Hierarchy” window in the Unity editor, select “3D Object,” and then “Cube.” We’ll resize the cube and add a material to it. The final scene should look like the following:

cube Scene
cube Scene

Scripting the movement

In this step, we will create a new C# script to handle the movement of a cube based on arrow key input. We will attach the script to the cube to respond to the user’s arrow key presses and move accordingly. To do that, we’ll perform the following steps:

  • We’ll create a new C# script in Unity by right-clicking on the “Project” window, choosing “Create,” and selecting “C# Script.” We’ll name it “CubeController” or any desired name, then press “Enter” to generate the script.

  • We’ll attach the script to the cube GameObject in the Scene Hierarchy. Then, we’ll access the “Project” window and click and drag the “CubeController” script onto the cube. This will attach the script to the cube, completing the process.

Horizontal movement only

The Input.GetAxis("Horizontal") reads user input from the horizontal axis, like left/right arrow keys, giving them values between -1 and 1. The -1 value means the left arrow key is pressed, 1 means the right arrow key is pressed, and 0 indicates no input. We’ll use this value to control the GameObject movement or rotation based on left-right input. Then, we’ll open the “CubeController” script in our code editor (e.g., Visual Studio) by double-clicking on it in the “Project” window and copying and pasting the given code into the script.

using UnityEngine;
public class CubeController : MonoBehaviour
{
public float speed = 5f; // Adjust this value to change the cube's movement speed.
void Update()
{
// Read the user's input from the arrow keys (left, right, up, down).
float horizontalInput = Input.GetAxis("Horizontal");
// Calculate the movement vector based on the arrow key input and speed.
Vector3 movement = new Vector3(horizontalInput, 0f, 0f) * speed * Time.deltaTime;
// Translate the cube's position based on the calculated movement.
transform.Translate(movement);
}
}


Vertical movement only

The Input.GetAxis("Vertical") function in Unity captures user input from the vertical axis, such as by pressing the up or down arrow keys. It returns values ranging from -1 to 1. When the down arrow key is pressed, the function returns -1; when the up arrow key is pressed, it returns 1; and when no input is detected, it returns 0. This allows developers to utilize the returned value to control the GameObject movement or rotation based on vertical input.

using UnityEngine;
public class CubeController : MonoBehaviour
{
public float speed = 5f; // Adjust this value to change the cube's movement speed.
void Update()
{
// Read the user's input from the arrow keys (up, down).
float verticalInput = Input.GetAxis("Vertical");
// Calculate the movement vector based on the arrow key input and speed.
Vector3 movement = new Vector3(0f, 0f, verticalInput) * speed * Time.deltaTime;
// Translate the cube's position based on the calculated movement.
transform.Translate(movement);
}
}

Horizontal and vertical movement

To enable both vertical and horizontal movement, we will include both Input.GetAxis("Horizontal") and Input.GetAxis("Vertical") in the script. To do that, we’ll open the “CubeController” script and copy and paste the provided code.

using UnityEngine;
public class CubeController : MonoBehaviour
{
public float speed = 5f; // Adjust this value to change the cube's movement speed.
void Update()
{
// Read the user's input from the arrow keys (left, right, up, down).
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
// Calculate the movement vector based on the arrow key input and speed.
Vector3 movement = new Vector3(horizontalInput, 0f, verticalInput) * speed * Time.deltaTime;
// Translate the cube's position based on the calculated movement.
transform.Translate(movement);
}
}

The script allows the cube GameObject to move on the x-z plane in response to the arrow key input from the user. The speed variable controls the movement speed and Time.deltaTime ensures that the movement is frame-rate independent, providing smooth and consistent movement across various devices.

In the Scene below, we can visualize the final output:

Note: To play the game, click the Run button below and wait a few seconds for the “Output” window to render. We can also play the game in a new tab by clicking the link below “Your app can be found at:”.

import React from 'react';
require('./style.css');

import ReactDOM from 'react-dom';
import App from './app.js';

ReactDOM.render(
  <App />, 
  document.getElementById('root')
);

We now have a cube that responds to arrow key input. Handling user input is important for game development and interactive experiences. To customize the cube’s behavior, experiment with the speed variable or other aspects of the movement script. You can also add features like movement boundaries or animations to improve user experience.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved