Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Why is unity not able to notice when my key is pressed and released really fast?

Discussion in 'Scripting' started by TNG-Divine, May 11, 2024.

  1. TNG-Divine

    TNG-Divine

    Joined:
    May 10, 2024
    Posts:
    1
    The animator's parameter for when I release the key does not turn to true if I release it really fast. Do you know why? Here is my code and animator transitions.

    - This is in my moveScript
    public void CharacterAim(bool isAiming)
    {
    anim.SetBool(animStrings.isAiming, isAiming);
    }

    public void CharacterAimRelease(bool aimRelease)
    {
    anim.SetBool(animStrings.aimRelease, aimRelease);
    }

    void Update()
    {

    moveScript.AnimateCharacter(Input.GetAxis(input.forwardInput));
    moveScript.Running(Input.GetButton(input.runningInput));
    moveScript.CharacterAim(Input.GetButton(input.aimInput));
    moveScript.CharacterAimRelease(Input.GetKeyUp(KeyCode.Z));



    }
    upload_2024-5-12_6-8-38.png
    upload_2024-5-12_6-8-54.png
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,445
    Debug it... debug your animation transitions so you know they work correctly before looking to the code.

    You might be pressing and releasing the key in the same frame, if that's even possible. At least eliminate that chance by gathering all the input into temp variables, printing them, then acting on those temp variables.

    You can also debug the input stream by replacing it: make an array of the inputs in code and send them off to the animator super-fast every frame, perhaps restarting the entire sequence by pressing another debug keypress.

    (By that last paragraph I mean stream in
     false, false, false, true, false, false 
    all in a row and make sure the animator does what it should. If it doesn't, you might need to buffer or delay the input intents.)
     
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    7,024
    This reads like a flawed approach where you have two bools for one binary action.

    The character is either aiming or isn't. There is no aiming and release aiming, those aren't separate actions. Imagine a typical game where you pull down LT on the gamepad to aim down sights. Is this two buttons or one? ;)