New Input System

Because the old input system is not working correctly for me.
I just wrote a static class that use the New Input system and mimics the old one :slight_smile:

#region

using UnityEngine;
using UnityEngine.InputSystem;

#endregion

public static class NewInput
{
    public enum Direction
    {
        Horizontal,
        Vertical
    }

    public static Vector3 mousePosition => Mouse.current.position.ReadValue();

    public static float GetAxis(Direction direction)
    {
        return direction switch
        {
            Direction.Horizontal => Keyboard.current[Key.A].isPressed ? -1f :
                Keyboard.current[Key.D].isPressed ? 1f : 0f,
            Direction.Vertical => Keyboard.current[Key.S].isPressed ? -1f : Keyboard.current[Key.W].isPressed ? 1f : 0f,
            _ => 0f
        };
    }

    public static bool GetKeyDown(KeyCode code)
    {
        return code switch
        {
            KeyCode.Space => Keyboard.current[Key.Space].wasPressedThisFrame,
            KeyCode.Escape => Keyboard.current[Key.Escape].wasPressedThisFrame,
            _ => false
        };
    }

    public static bool GetKeyUp(KeyCode code)
    {
        return code switch
        {
            KeyCode.Space => Keyboard.current[Key.Space].wasReleasedThisFrame,
            KeyCode.Escape => Keyboard.current[Key.Escape].wasReleasedThisFrame,
            _ => false
        };
    }

    public static bool GetKey(KeyCode code)
    {
        return code switch
        {
            KeyCode.Space => Keyboard.current[Key.Space].isPressed,
            KeyCode.Escape => Keyboard.current[Key.Escape].isPressed,
            _ => false
        };
    }

    public static bool GetMouseButtonDown(int buttonNumber)
    {
        return buttonNumber switch
        {
            0 => Mouse.current.leftButton.wasPressedThisFrame,
            1 => Mouse.current.rightButton.wasPressedThisFrame,
            2 => Mouse.current.middleButton.wasPressedThisFrame,
            _ => false
        };
    }

    public static bool GetMouseButtonUp(int buttonNumber)
    {
        return buttonNumber switch
        {
            0 => Mouse.current.leftButton.wasReleasedThisFrame,
            1 => Mouse.current.rightButton.wasReleasedThisFrame,
            2 => Mouse.current.middleButton.wasReleasedThisFrame,
            _ => false
        };
    }

    public static bool GetMouseButton(int buttonNumber)
    {
        return buttonNumber switch
        {
            0 => Mouse.current.leftButton.isPressed,
            1 => Mouse.current.rightButton.isPressed,
            2 => Mouse.current.middleButton.isPressed,
            _ => false
        };
    }
}

Now just import the New Imput system package,
and replace the Input. with NewInput.
And for the get axis β€œfloat moveX = NewInput.GetAxis(NewInput.Direction.Horizontal);”

So, until the input system is tackled in this course, you can use this.

Greetings Bert.

Privacy & Terms