Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære From Player Input to Player Movement | Fundamentals of Object Oriented Programming with C#
Unity for Beginners

bookFrom Player Input to Player Movement

Sveip for å vise menyen

Player Movement: Adding Player Input

In this lesson, you turn the project into a playable game by adding player input and using it to move the player left and right.

With just this script, you can already build a playable level.

Checking Input in Update()

Player input needs to be checked every frame, so all input logic belongs in the Update() function.

To check if a key is being pressed, Unity's classic input system is used:

Input.GetKey(KeyCode.A)
  • GetKey is true for as long as the key is held down;
  • GetKeyDown is only true for the single frame the key is first pressed.

For continuous movement, GetKey is the correct choice.

Enabling the Classic Input System

If you see an error when using Input.GetKey, Unity may be set to use only the New Input System.

To fix this:

  • save your project;
  • go to Edit -> Project Settings -> Player;
  • set Active Input Handling to Both;
  • let Unity restart.

Accessing the Rigidbody2D

To move the player using physics, we need a reference to the player's Rigidbody2D.

A common approach is to keep it private and assign it in Start():

rigidbody2D = gameObject.GetComponent<Rigidbody2D>();

This tells Unity to:

  • look at the GameObject the script is attached to;
  • get its Rigidbody2D component.

Moving the Player on the X Axis

Using our pseudocode, we now change the player's velocity:

  • press A -> move left (negative x);
  • press D -> move right (positive x).

This is done by setting the rigidbody's x-axis velocity using movementSpeed.

Stopping the Player When No Key Is Pressed

At first, the player keeps moving even after input stops.

This happens because we never reset the velocity.

To fix this:

  • check if A is NOT pressed;
  • AND D is NOT pressed;
  • then set the x velocity to 0.

This introduces:

  • ! ('NOT' operator);
  • && (logical 'AND' operator).

Alternative: Using Unity's Horizontal Axis

Unity also provides a built-in input axis called Horizontal.

Instead of checking keys manually, you can use:

Input.GetAxis("Horizontal")
  • returns values between -1 and 1;
  • automatically maps to A / D and Left / Right arrows.

Multiplying this by movementSpeed gives smooth movement with acceleration and deceleration.

You can adjust how snappy or smooth this feels in:

  • Project Settings -> Input Manager.

  • Gravity: how quickly movement stops;

  • Sensitivity: how quickly movement starts.

Result

You now have:

  • player input;
  • physics-based movement;
  • a controllable character.

1. Why is player input checked inside the Update() function instead of Start()?

2. What is the main difference between Input.GetKey() and Input.GetKeyDown() in Unity?

question mark

Why is player input checked inside the Update() function instead of Start()?

Velg det helt riktige svaret

question mark

What is the main difference between Input.GetKey() and Input.GetKeyDown() in Unity?

Velg det helt riktige svaret

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 3

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Seksjon 3. Kapittel 3
some-alt