CodeBlanks Logo Icon CodeBlanks

Movement vector

Learn to turn axis inputs into a movement Vector3.

Combining Axes with Vector3

In character movement, horizontal and vertical axis inputs are combined into a Vector3 variable. The horizontal axis (X) controls left/right movement, while the vertical axis (Z) controls forward/backward movement in a 3D world. The Y axis is typically reserved for gravity and jumping.

float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = new Vector3(x, 0f, z);

Applying the Movement Vector

The constructed movement vector is applied to the object using the previously learned transform.Translate method. Multiplying by a speed variable adjusts the movement rate. This allows the object to move smoothly according to the keys pressed by the player.

Vector3 move = new Vector3(x, 0f, z);
transform.Translate(move * speed * Time.deltaTime);

Practice Calculate Movement Vector

Take Input.GetAxis("Horizontal") and Input.GetAxis("Vertical"), combine them with new Vector3(yatay, 0f, dikey). Print the result using Debug.Log(hareket);.

Starter code

Player.cs
using System;

public struct Vector3
{
    public float x, y, z;
    public Vector3(float x, float y, float z) { this.x = x; this.y = y; this.z = z; }
    public override string ToString() => $"({x}, {y}, {z})";
}

public static class Input
{
    public static float GetAxis(string axis) => axis == "Horizontal" ? 1.0f : 2.0f;
}

public static class Debug
{
    public static void Log(object message) => Console.WriteLine(message);
}

public class Program
{
    public static void Main()
    {
        // yatay ve dikey eksenleri oku, Vector3 olustur ve Debug.Log ile yazdir
    }
}

Sample solution

Player.cs
using System;

public struct Vector3
{
    public float x, y, z;
    public Vector3(float x, float y, float z) { this.x = x; this.y = y; this.z = z; }
    public override string ToString() => $"({x}, {y}, {z})";
}

public static class Input
{
    public static float GetAxis(string axis) => axis == "Horizontal" ? 1.0f : 2.0f;
}

public static class Debug
{
    public static void Log(object message) => Console.WriteLine(message);
}

public class Program
{
    public static void Main()
    {
        float yatay = Input.GetAxis("Horizontal");
        float dikey = Input.GetAxis("Vertical");
        Vector3 hareket = new Vector3(yatay, 0f, dikey);
        Debug.Log(hareket);
    }
}

Expected output

(1, 0, 2)

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 In a 3D game, which Vector3 component is mapped to axis input for moving forward and backward on the ground?

  • W component
  • X component
  • Y component
  • Z component Correct answer

Explanation: In 3D space, X is horizontal (right-left), Y is vertical height (up-down), and Z is depth (forward-backward).

2 Create 3D Movement Vector

Question
Vector3 hareket = new ____(yatay, 0f, dikey);
Answer
Vector3 hareket = new Vector3(yatay, 0f, dikey);

Explanation: To create a 3-dimensional vector with X, Y, and Z components, new Vector3(...) is used.

3 Vector Components Matching

Term Meaning
Vector3.x Right and left horizontal axis input
Vector3.y Height and jumping axis
Vector3.z Forward and backward depth axis
transform.Translate Translates object in vector direction

All lessons in Beginner