0 of 16 lessons complete (0%)
Essential Activities

Moving a player with the new Unity Input System

You don’t have access to this lesson
Please register or sign in to access the course content.
Length: 45 minutes|Difficulty: Standard

The new input system[1] provides a number of advantages over the original input system.

The main one is that it can enable local multiplayer easily and that you can create events to handle button inputs[2] that are run only when the button is pressed and not on every update.

You can also update the input actions from a single location and this will make the changes across all parts of the program that use this set of inputs.

First add the new input system and create default input actions.

Install the Input system package and create the default input actions for the player object.

Add code to your player object using the Input System[1] of Unity.

Any movement of objects should be handled in the FixedUpdate method.

Note replace the Update method with FixedUpdate.

The code below is what is created in the video above.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

[RequireComponent(typeof(CharacterController))]
public class PlayerMovement : MonoBehaviour
{
    // Player variables
    [SerializeField] private float _speed = 5.0f;
    private Vector2 _movementInputVector;

    private CharacterController _characterContoller;

    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("Player created!");
        _characterContoller = GetComponent<CharacterController>();
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        MovePlayer();
    }

    void MovePlayer() {
        Vector3 move = new Vector3(_movementInputVector.x, 0.0f, _movementInputVector.y);

        _characterContoller.SimpleMove(move * _speed);
    }

    void OnMove(InputValue iv) {
        _movementInputVector = iv.Get<Vector2>();
    }
}

Terms definitions
2. inputs. If you do something, and a computer or system ‘receives’ that, you’ve given it input. If that input triggers a function[3], you might receive some kind of feedback[4] from the system.
Terms definitions
1. system. Length: 45 minutes|Difficulty: Standard The new input system provides a number of advantages over the original input system. The main one is that it can enable local multiplayer easily and that you can create events to handle button inputs that are run only when the button is pressed and not on every update. You can […]
2. inputs. Length: 45 minutes|Difficulty: Standard The new input system provides a number of advantages over the original input system. The main one is that it can enable local multiplayer easily and that you can create events to handle button inputs that are run only when the button is pressed and not on every update. You can […]
3. function. Length: 45 minutes|Difficulty: Standard The new input system provides a number of advantages over the original input system. The main one is that it can enable local multiplayer easily and that you can create events to handle button inputs that are run only when the button is pressed and not on every update. You can […]
4. feedback. Length: 45 minutes|Difficulty: Standard The new input system provides a number of advantages over the original input system. The main one is that it can enable local multiplayer easily and that you can create events to handle button inputs that are run only when the button is pressed and not on every update. You can […]