• Hello
Search Results for

    Show / Hide Table of Contents

    Protected Input (Pro)

    Auto clickers and macros press a button faster than a person can. That matters in clicker games, rhythm games, weapons, and fast menus. ProtectedInput limits how often each button counts as pressed. An auto clicker then gains nothing.

    Protected input needs the Pro version.

    Walkthrough: Protecting against auto clickers.

    Note

    You need the Input System package (com.unity.inputsystem, version 1.1 or newer). In Edit > Project Settings > Player, set Active Input Handling to Input System Package (New) or Both. Without that, ProtectedInput does not exist in your project.

    How to use

    Replace the "pressed this frame" calls of the Input System with ProtectedInput (namespace GUPS.AntiCheat.Protected.Input).

    Before:

    if (Mouse.current.leftButton.wasPressedThisFrame)
    {
        Shoot();
    }
    

    After:

    using GUPS.AntiCheat.Protected.Input;
    using UnityEngine.InputSystem.LowLevel;
    
    // Fires at most 10 times per second, no matter how fast the mouse is clicked.
    if (ProtectedInput.GetMouseButtonDown(MouseButton.Left))
    {
        Shoot();
    }
    

    The calls

    Pressed this frame. These are limited:

    Call Replaces
    GetKeyDown(Key.Space) Keyboard.current[Key.Space].wasPressedThisFrame
    GetMouseButtonDown(MouseButton.Left) Mouse.current.leftButton.wasPressedThisFrame
    GetGamepadButtonDown(GamepadButton.South) Gamepad.current.buttonSouth.wasPressedThisFrame
    GetTouchDown(0) A new touch on the screen
    WasPressedThisFrame(action) action.WasPressedThisFrame()
    OnPerformed(action, callback) action.performed += callback

    Held down. These are passed on as they are and not limited:

    Call What it tells you
    GetKey, GetMouseButton, GetGamepadButton, GetTouch The key, button, or touch is held right now
    IsPressed(action) The action is held right now

    Every call takes an optional device as its last parameter. Without it, the current device is used, for example Keyboard.current.

    Input actions

    Actions from an Input Actions asset are limited the same way. That includes PlayerInput and your own action maps.

    When you read the action yourself, pass it to ProtectedInput:

    // Before: if (attackAction.WasPressedThisFrame())
    if (ProtectedInput.WasPressedThisFrame(attackAction))
    {
        Attack();
    }
    

    When you use the performed event, subscribe through ProtectedInput.OnPerformed. It returns a handle. Dispose it to stop listening.

    using System;
    using GUPS.AntiCheat.Protected.Input;
    using UnityEngine;
    using UnityEngine.InputSystem;
    
    public class Player : MonoBehaviour
    {
        private InputSystem_Actions actions;
        private IDisposable attackSubscription;
    
        private void Awake()
        {
            actions = new InputSystem_Actions();
        }
    
        private void OnEnable()
        {
            actions.Player.Enable();
    
            // Before: actions.Player.Attack.performed += _ => Attack();
            attackSubscription = ProtectedInput.OnPerformed(actions.Player.Attack, _ => Attack());
        }
    
        private void OnDisable()
        {
            attackSubscription?.Dispose();
            actions.Player.Disable();
        }
    }
    

    With a PlayerInput component, use ProtectedInput.OnPerformed(playerInput.actions["Attack"], ...) the same way.

    Local multiplayer

    Each button is limited on its own device. The south button of two gamepads is two inputs, so player two is never slowed down by player one.

    Pass the device of the player:

    public class CoopPlayer : MonoBehaviour
    {
        public Gamepad Gamepad; // for example from PlayerInput.devices or Gamepad.all[index]
    
        private void Update()
        {
            if (ProtectedInput.GetGamepadButtonDown(GamepadButton.South, Gamepad))
            {
                Attack();
            }
        }
    }
    

    PlayerInput gives each player its own copy of the actions. So WasPressedThisFrame(playerInput.actions["Attack"]) is already separate for each player.

    How the limit works

    • Each button on each device has its own limit. The default is 10 presses per second. That is one press every 100 ms.
    • You set the limit as Max Presses Per Second on the Input Cheating Detector. In code you read it from ProtectedInput.MaxPressesPerSecond.
    • A press that comes a little too early is not lost. It counts as soon as the wait is over. A person who clicks slightly too fast loses nothing.
    • Extra presses during the wait are folded into one. An auto clicker at 100 clicks per second becomes 10 clicks per second.
    • The limit does not depend on the frame rate.
    • The calls work in Update, LateUpdate, and FixedUpdate. In FixedUpdate, a press is seen by exactly one physics step, never missed and never twice.

    Do you need to add the detector?

    ProtectedInput works with the InputCheatingDetector and its InputMonitor. They hold the limit and report an auto clicker to the Anti-Cheat Monitor.

    Most of the time you have nothing to do. The predefined Anti-Cheat-Monitor prefab already includes both.

    If you use your own monitor prefab, drag the Input Cheating Detector prefab onto the monitor in your prefab so it becomes a child. It holds the detector and the monitor together.

    The detector prefabs in Assets/GUPS/AntiCheat/Resources/Prefabs/Detectors.

    Without the detector, ProtectedInput still limits each input to 10 presses per second. No cheat is reported, and a warning is logged once.

    Settings: Input monitor and Input detector.

    Good to know

    • Button actions only. A value action, such as a Vector2 Move action, changes all the time and is not counted as a press.
    • An action and its button are separate. WasPressedThisFrame(attackAction) and GetMouseButtonDown(MouseButton.Left) do not share a limit, even when Attack is bound to the left mouse button. Use one of the two for the same thing.
    • Coming from the old Input class. Replace KeyCode with Key. KeyCode.Space becomes Key.Space, KeyCode.LeftControl becomes Key.LeftCtrl, and KeyCode.Alpha1 becomes Key.Digit1. For joystick buttons use GetGamepadButtonDown.
    • Mouse and gamepad buttons. MouseButton and GamepadButton are in UnityEngine.InputSystem.LowLevel.

    React to an auto clicker

    Ask the detector in code:

    var detector = AntiCheatMonitor.Instance.GetDetector<InputCheatingDetector>();
    
    if (detector != null && detector.PossibleCheatingDetected)
    {
        // A button was pressed faster than a person can.
    }
    

    More ways to react: Reacting to cheating.


    Back to the Protected overview.

    In This Article
    Back to top GuardingPearSoftware documentation