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[5] to add the input system[6].

Open the Package Manager

Window > Package Manager

Click on Packages: In Project[7] and select Unity Registry

Search for Input System[6]

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[8] that we want to use for the player.

Select the Player objects.

From the inspector click Add Component[1]

Add a Player Input component[1]

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[2] 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[3] assign the rigidbody of the object this script is attached to (the player) to the variable[4] 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);
    }
}
Terms definitions
2. variables. “Containers” used to label and store data[12] in memory. The data can then be used throughout a program: * Integer[13] variables use only numbers and so can be used for calculations * String[14] variables are sequences of code that may contain numbers, letters, and other characters, and so cannot be used for calculations.
3. function. A named section of a computer program that performs a specific task. Functions help make code more efficient and reusable. They may take input parameters and produce output.
4. variable. A placeholder for a piece of information that can change.
5. need. An identified requirement of a person, group, or environment[10]. A need is identified from an issue[15] and sits within a context[16]. Technological practice[9] can be undertaken in an attempt to meet an identified need.
Terms definitions
1. Component. 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 […]
2. variables. 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 […]
3. function. 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 […]
4. variable. 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 […]
5. need. 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 […]