Player Controls (New Input System)

Moving the player using the Input System

You don’t have access to this lesson
Please register or sign in to access the course content.

Create a scene with a ground and a player object.

The example below has a plane for the ground and a capsule for the player.

The capsule has been renamed Player and has had a rigid body applied to it.

Gravity for the RigidBody on the Capsule is turned off

The player capsule is also transformed on the y axis so it is above the ground plane.

Now we need to add the input system.

Open the Package Manager

Window > Package Manager

Click on Packages: In Project and select Unity Registry

Search for Input System

Click install

Wait until the install completes

You will get a message saying Unity needs to restart to enable the new input system. Click Yes

Now we need to set up the inputs that we want to use for the player.

Select the Player objects.

From the inspector click Add Component

Add a Player Input component

To set up the default inputs click Create Actions.

Give the Input actions a name and save the file.

Drag this InputActions file to the actions of Player Input on the Player game object.

Now we need to create a script to handle inputs.

Create a script called PlayerController

Drag this script onto the player object

Open the file in Visual Studio or your editor of choice.

Add using UnityEngine.InputSystem to the script to allow the script to use the InputSystem.

Add the following variables to the script

speed, for how fast the player will move

RigidBody rb for the rigidbody of the player

movementX and movementY for the x and y movement.

In the start function assign the rigidbody of the object this script is attached to (the player) to the variable rb.

Create a function OnMove as shown below.

This function takes an InputValue and then converts it to a 2D vector and stores this in movementX and movementY.

Finally replace the Update method with FixedUpdate.

FixedUpdate will update at regular intervals.

This functions creates a 3D Vector for movement and then applies a force to the object.

Note that the initial speed is set to 0. Change this to another number, for example 5.

You now have a system that respond to default keyboard and controller input.

To view the input settings open the Default Input Actions file

You can customise this from in here.

Below is the complete PlayerController script.

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

public class PlayerController : MonoBehaviour
{
    public float speed = 0;

    private Rigidbody rb;

    private float movementX;
    private float movementY;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    private void OnMove(InputValue movementValue)
    {
        Vector2 movementVector = movementValue.Get<Vector2>();

        movementX = movementVector.x;
        movementY = movementVector.y;
    }

    private void FixedUpdate()
    {
        Vector3 movement = new Vector3(movementX, 0.0f, movementY);

        rb.AddForce(movement * speed);
    }
}