Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Discussion Input System can crash your application if you aren't careful with this.

Discussion in 'Input System' started by mihairobert466, Apr 18, 2024.

  1. mihairobert466

    mihairobert466

    Joined:
    Oct 5, 2018
    Posts:
    2
    Hi everyone!

    I am developing a small game and I am using Input System package to implement the character movement and all the actions in my game.

    I am working right now to do some stealth mechanics for player's character and ran into a huge issue with the Input System that caused crashes and frame drops from 200+ fps to 5-10 fps.

    My player has a state management system so that all the behaviour is easy to follow, the code is clean and organised and it can't have weird combinations of states since only one state is active at a time.

    I've created a Crouching State that looks like this:

    CrouchPlayerState
    Code (CSharp):
    1. using Behaviour.StateMachine;
    2. using UnityEngine;
    3.  
    4. public class CrouchPlayerState : BaseState
    5. {
    6.     private PlayerInputActions playerInput;
    7.     private readonly int animationHash = Animator.StringToHash("IsCrouching");
    8.  
    9.     public CrouchPlayerState(BaseStateHandler owner) : base(owner)
    10.     {
    11.     }
    12.  
    13.     public override void Start()
    14.     {
    15.         base.Start();
    16.         Debug.Log("CrouchPlayerState ");
    17.         playerInput = new PlayerInputActions();
    18.         playerInput.PlayerDefaultMovement.Enable();
    19.         Owner.SetAnimationBool(animationHash, true);
    20.     }
    21.  
    22.     public override void Update()
    23.     {
    24.         base.Update();
    25.         if (PlayerTriesToMove())
    26.         {
    27.             ChangeState(new CrouchWalkingState(Owner));
    28.         }
    29.     }
    30.  
    31.     private bool PlayerTriesToMove()
    32.     {
    33.         var inputValue = playerInput.PlayerDefaultMovement.Movement.ReadValue<Vector2>();
    34.         if (!inputValue.Equals(Vector2.zero))
    35.         {
    36.             return true;
    37.         }
    38.         return false;
    39.     }
    40.  
    41.     public override void Exit(IBaseState newState)
    42.     {
    43.         base.Exit();
    44.  
    45.         if (newState is not CrouchPlayerState)
    46.         {
    47.             Owner.SetAnimationBool(animationHash, false);
    48.         }
    49.     }
    50. }
    and a walking crouch state that looks like this

    CrouchWalkingState

    Code (CSharp):
    1. using Behaviour.StateMachine;
    2. using UnityEngine;
    3.  
    4. public class CrouchWalkingState : CrouchPlayerState
    5. {
    6.     readonly int animationHash = Animator.StringToHash("IsRunning");
    7.     private PlayerMovement playerMovement;
    8.  
    9.     public CrouchWalkingState(BaseStateHandler owner) : base(owner)
    10.     {
    11.     }
    12.  
    13.     public override void Start()
    14.     {
    15.         base.Start();
    16.         Debug.Log("CrouchPlayerState ");
    17.         playerMovement = GetComponent<PlayerMovement>();
    18.         playerMovement.SetSpeed(2);
    19.         Owner.SetAnimationBool(animationHash, true);
    20.     }
    21.  
    22.     public override void Update()
    23.     {
    24.         base.Update();
    25.         if (playerMovement.HasMovementInputValue())
    26.             playerMovement.DefaultMovement();
    27.     }
    28. }
    The issue here, if you can spot the bug, is that CrouchWalkingState inherits CrouchPlayerState and I am including the base functionality in both my Start and Update functions.

    This caused a lot of instancing and enabling for Input System class.

    If you find your system having this issue where it shows you that the huge spike in the CPU usage is coming from
    NativeInputSystem.NotifyUpdate()
    and
    InputUpdate()
    :

    Just know that initialising the Input System class and enabling it inside a loop will crash your app. It looks like these operations are really expensive!
     
    Last edited: Apr 18, 2024