Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Working with conditions in Update / FixedUpdate?

Discussion in 'Input System' started by TimHellmann, Mar 4, 2020.

  1. TimHellmann

    TimHellmann

    Joined:
    Mar 6, 2011
    Posts:
    66
    Dear beloved community and experts.
    I have been attempting to further optimize my controls code so that I have one script for all input that only calls and recognizes the Inputs if certain bools / if-conditions are true.
    It is crucial for the project as I have multiple playable characters, like in "Thomas was alone". So I check which of the player characters is currently active, for multiple reasons.

    I wrapped an if-condition around the Inputs in the Update of that central controls script.
    Now, the new input system, from what I know so far, links any Input to its method in Awake.
    However, is it still possible to get the new system to run and detect stuff in Update as I show you in my code example below?
    Or do I need to redo that outer if-condition and have to check it in the individual methods now?

    Code (CSharp):
    1.  
    2. void Update () {
    3.  
    4. // This if-check is the crucial part.
    5.         if (player.activeSelection && player.canMove)
    6.         {      
    7.             // Move
    8.             if ((Input.GetAxisRaw("Horizontal") < -0.1f
    9.             || Input.GetAxisRaw("Horizontal") > 0.1f)
    10.             {
    11.                     player.Move();
    12.             }
    13.             else
    14.             {
    15.                 player.MoveStop();
    16.                 player.isMoving = false;
    17.             }
    18.  
    19.             // Run
    20.             if (Input.GetButton("Run"))
    21.                 player.anim.SetBool("runButton", true);
    22.             else
    23.                 player.anim.SetBool("runButton", false);
    24.  
    25.  
    26.             // HOLD RUN BUTTON & MOVE LEFT / RIGHT
    27.             if (Input.GetButton("Run") && (Input.GetAxisRaw("Horizontal") != 0))
    28.             {
    29.                 player.Sneak();
    30.             }
    31.             else
    32.             {
    33.                 player.maxSpeed = player.moveSpeed;
    34.                 player.isSneaking = false;
    35.             }
    36.  
    37.             // DROP ITEM
    38.             if (inventory.hasInventoryItem && Input.GetButtonDown("Use"))
    39.             {
    40.                 player.DropItem();
    41.             }
    42.  
    43.             // DOUBLE JUMP
    44.             if (Input.GetButtonDown("Jump") && player.isDoubleJumpReady)
    45.                 player.DoubleJump();
    46.  
    47.             // Jump
    48.             if (player.canJump)
    49.             {
    50.                 if (Input.GetButtonDown("Jump") && player.isGrounded)
    51.                 {
    52.                     player.Jump();
    53.                 }
    54.             }
    55.  
    56.             // GRAB ITEM
    57.             if (player.rayEyes.collider != null && Input.GetButtonDown("Use"))
    58.                 player.GrabItem();
    59.  
    60.             // FIRE WEAPON
    61.             if (Input.GetButtonDown("Fire"))
    62.             {
    63.                 if (inventory.hasInventoryItem)
    64.                 {
    65.                     if (inventory.InventoryItem.CompareTag("weapon"))
    66.                     {
    67.                         playerRat.FireGun();
    68.                     }
    69.                 }
    70.                 else
    71.                 {
    72.                     if (player.canPunch)
    73.                     {
    74.                         player.DoPunch();
    75.                     }
    76.                 }
    77.             }
    78.          }
    79.     }
    Help is much appreciated.
     
    Last edited: Mar 4, 2020