Last active
April 18, 2022 19:47
-
-
Save brihernandez/4dce6f34625e297fb7950d36957d93cd to your computer and use it in GitHub Desktop.
Example of how to use the new Input System in Unity. This is how I handled input in Vector Fury.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is an example of how to use the new input system in a way that I think is actually | |
// usable for a production game, and not just rapid prototyping. This is much more stable, | |
// and because it uses the C# class version of the new Input System it will fail to compile | |
// if something changes in the input asset, which is a VERY GOOD THING. | |
using UnityEngine; | |
public class CameraInput | |
{ | |
public float Pitch = 0f; | |
public float Yaw = 0f; | |
public bool ResetCamera = false; | |
} | |
public class FlightStick | |
{ | |
public float Pitch = 0f; | |
public float Yaw = 0f; | |
public float Roll = 0f; | |
public float Throttle = 0.8f; | |
public float Nozzle = 1f; | |
public bool IsFiring = false; | |
public bool IsWeaponReleaseActive = false; | |
public System.Action OnWeaponReleased; | |
public void Reset() | |
{ | |
Pitch = 0f; | |
Yaw = 0f; | |
Roll = 0f; | |
Throttle = 0.8f; | |
Nozzle = 1f; | |
} | |
} | |
public static class PlayerInput | |
{ | |
// VectorInput is the Input System asset that contains the action map | |
private static VectorInput inputs; | |
public static FlightStick FlightInput { get; private set; } = null; | |
public static CameraInput CameraInput { get; private set; } = null; | |
private static float throttleSpeed = 0f; | |
private static float nozzleSpeed = 0f; | |
[System.Diagnostics.CodeAnalysis.SuppressMessage( | |
"Performance", "CA1810:Initialize reference type static fields inline", | |
Justification = "Needs additional setup in constructor.")] | |
static PlayerInput() | |
{ | |
// A really nice thing about the C# class version of the Input System assets is that you | |
// can get all the functionality of them by just creating an instance of them. | |
inputs = new VectorInput(); | |
FlightInput = new FlightStick(); | |
CameraInput = new CameraInput(); | |
// Most inputs are simple buttons and can be set up in an event driven way to set | |
// the necessary variables or call the necessary events. | |
inputs.Flight.ReleaseWeapon.started += context => | |
{ | |
FlightInput.OnWeaponReleased?.Invoke(); | |
FlightInput.IsWeaponReleaseActive = true; | |
}; | |
inputs.Flight.ReleaseWeapon.performed += context => FlightInput.IsWeaponReleaseActive = true; | |
inputs.Flight.ReleaseWeapon.canceled += context => FlightInput.IsWeaponReleaseActive = false; | |
inputs.Flight.FireGun.performed += context => FlightInput.IsFiring = true; | |
inputs.Flight.FireGun.canceled += context => FlightInput.IsFiring = false; | |
inputs.Flight.ThrottleUp.performed += context => throttleSpeed = 2f; | |
inputs.Flight.ThrottleUp.canceled += context => throttleSpeed = 0f; | |
inputs.Flight.ThrottleDown.performed += context => throttleSpeed = -2f; | |
inputs.Flight.ThrottleDown.canceled += context => throttleSpeed = 0f; | |
inputs.Flight.NozzleUp.performed += context => nozzleSpeed = 1f; | |
inputs.Flight.NozzleUp.canceled += context => nozzleSpeed = 0f; | |
inputs.Flight.NozzleDown.performed += context => nozzleSpeed = -1f; | |
inputs.Flight.NozzleDown.canceled += context => nozzleSpeed = 0f; | |
inputs.Flight.ResetView.started += context => CameraInput.ResetCamera = true; | |
inputs.Flight.ResetView.canceled += context => CameraInput.ResetCamera = false; | |
} | |
public static void ResetInput() | |
{ | |
FlightInput.Reset(); | |
} | |
public static void UpdateInput(float deltaTime) | |
{ | |
// Sometimes, your input will need to update something over time. For those, I | |
// just used a simple update function. Note though that since this is a static | |
// class and NOT a Monobehaviour, you will need some object to constantly call | |
// this as part of your game's update loop. In Vector Fury there was a | |
// GameLogic Monobehaviour which was always present in the flying gameplay scene | |
// which called it. | |
FlightInput.Throttle += throttleSpeed * deltaTime; | |
FlightInput.Throttle = Mathf.Clamp(FlightInput.Throttle, 0f, 1f); | |
FlightInput.Nozzle += nozzleSpeed * deltaTime; | |
FlightInput.Nozzle = Mathf.Clamp(FlightInput.Nozzle, 0f, 1f); | |
// Some input works cleaner if it's just polled every frame. | |
var rawStick = inputs.Flight.Stick.ReadValue<Vector2>(); | |
FlightInput.Pitch = rawStick.y; | |
FlightInput.Roll = rawStick.x; | |
FlightInput.Yaw = inputs.Flight.Yaw.ReadValue<float>(); | |
rawStick = inputs.Flight.Look.ReadValue<Vector2>(); | |
CameraInput.Pitch = rawStick.y; | |
CameraInput.Yaw = rawStick.x; | |
} | |
// A really awesome feature of the new Input System is being able to activate/deactive maps on | |
// the fly. The following two functions are used to enable/disable the Flight map, which prevents | |
// any flight controls from reporting input while the game is paused. | |
public static void EnableFlightControls() | |
{ | |
inputs.Flight.Enable(); | |
} | |
public static void DisableFlightControls() | |
{ | |
inputs.Flight.Disable(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment