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.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

How can I read input done in Update() inside a variable to be used on FixedUpdate()?

Discussion in 'Scripting' started by GuyStreamsStuff, Apr 19, 2018.

  1. GuyStreamsStuff

    GuyStreamsStuff

    Joined:
    Dec 22, 2017
    Posts:
    27
    Hey guys!

    So in my game I had problems registering inputs and after some research I found out my issue was that my inputs were in FixedUpdate(). Everyone says that it should be in Update() but I never found how to read input in Update() and then transfer that information to FixedUpdate(). I tried using bool as follows but the result feels the same:

    Code (CSharp):
    1.     bool AButtonDown;
    2.     bool AButtonUp;
    3.  
    4.     bool XButtonDown;
    5.     bool XButtonUp;
    6.  
    7.     private void Update()
    8.     {
    9.         if (Input.GetButtonDown("Fire1"))
    10.         {
    11.             AButtonDown = true;
    12.         }
    13.  
    14.         else
    15.         {
    16.             AButtonDown = false;
    17.         }
    18.  
    19.  
    20.         if (Input.GetButtonUp("Fire1"))
    21.         {
    22.             AButtonUp = true;
    23.         }
    24.  
    25.         else
    26.         {
    27.             AButtonUp = false;
    28.         }
    29.  
    30.         if (Input.GetButtonDown("xBut"))
    31.         {
    32.             XButtonDown = true;
    33.         }
    34.  
    35.         else
    36.         {
    37.             XButtonDown = false;
    38.         }
    39.  
    40.  
    41.         if (Input.GetButtonUp("xBut"))
    42.         {
    43.             XButtonUp = true;
    44.         }
    45.  
    46.         else
    47.         {
    48.             XButtonUp = false;
    49.         }
    50.  
    51.     }
    52.  
    53.  

    Then in fixed update I just do as follows:

    Code (CSharp):
    1.     void FixedUpdate()
    2.     {
    3.  
    4.         Ray ray = new Ray(transform.position, -transform.up);
    5.  
    6.         RaycastHit hit;
    7.  
    8.         if (!Physics.Raycast(ray, out hit, 1.8f))
    9.         {
    10.             if (hasHomingAttacked == false)
    11.             {
    12.                 chargeState = 0f;
    13.                 if (AButtonDown == true)
    14.                 {
    15.                     AButtonDown = false;
    16.                     character.AddForce(transform.forward * homingSpeed * Time.deltaTime, ForceMode.Impulse);
    17.                     character.AddForce(transform.up * homingSpeed * 0.5f * Time.deltaTime, ForceMode.Impulse);
    18.  
    19.                     hasHomingAttacked = true;
    20.                     animator.SetBool("isHoming", true);
    21.                     chargeState = 0f;
    22.  
    23.                 }
    24.  
    25.                 if (XButtonDown == true)
    26.                 {
    27.                     XButtonDown = false;
    28.                     character.AddForce(transform.up * -homingSpeed * Time.deltaTime, ForceMode.Impulse);
    29.                     hasHomingAttacked = true;
    30.                     chargeState = 0f;
    31.                 }
    32.             }
    33.         }
    34.  
    35.         else
    36.         {
    37.  
    38.             if (hit.collider.gameObject.tag == "Ground")
    39.             {
    40.                 hasHomingAttacked = false;
    41.  
    42.  
    43.                 if (chargeState == 0f)
    44.                 {
    45.                     GetComponent<TrailRenderer>().enabled = false;
    46.                     GetComponent<Dir>().modif = 1f;
    47.                     character.drag = 3f;
    48.                     sphere.GetComponent<MeshRenderer>().enabled = false;
    49.                     animator.SetBool("IsRolling", false);
    50.                     animator.SetBool("isCharging", false);
    51.  
    52.                     if (XButtonDown == true)
    53.                     {
    54.                         XButtonDown = false;
    55.                         GetComponent<TrailRenderer>().enabled = true;
    56.                         chargeState = 1f;
    57.                         GetComponent<Dir>().modif = 0.2f;
    58.                         sphere.GetComponent<MeshRenderer>().enabled = true;
    59.  
    60.                     }
    61.                 }
    62.  
    63.  
    64.                 if (chargeState == 1f)
    65.                 {
    66.                     animator.SetBool("isCharging", true);
    67.                     if (Input.GetButton("xBut"))
    68.                     {
    69.  
    70.                         spinDashMulti = spinDashMulti + 0.07f;
    71.  
    72.                         if (spinDashMulti > 3f)
    73.                         {
    74.                             spinDashMulti = 3f;
    75.                         }
    76.  
    77.                         character.drag = 3f;
    78.                     }
    79.  
    80.                     if (XButtonUp == true)
    81.                     {
    82.  
    83.                         chargeState = 2f;
    84.                         animator.SetBool("isCharging", false);
    85.                         animator.SetBool("IsRolling", true);
    86.                     }
    87.  
    88.  
    89.  
    90.                 }
    91.  
    92.  
    93.                 if (chargeState == 2f)
    94.                 {
    95.                     if (XButtonUp == true)
    96.                     {
    97.                         XButtonUp = false;
    98.                         character.drag = 1f;
    99.                         GetComponent<Dir>().modif = 0.2f;
    100.  
    101.                         character.AddForce(transform.forward * homingSpeed * spinDashMulti * Time.deltaTime, ForceMode.Impulse);
    102.  
    103.                         spinDashMulti = 0.2f;
    104.  
    105.  
    106.  
    107.  
    108.                     }
    109.  
    110.                     if (GetComponent<Dir>().runspeed < 5)
    111.                     {
    112.                         chargeState = 3f;
    113.                     }
    114.  
    115.                     if (XButtonDown == true)
    116.                     {
    117.                         XButtonDown = false;
    118.                         chargeState = 3f;
    119.                         GetComponent<Dir>().modif = 1f;
    120.                         character.drag = 5f;
    121.                         GetComponent<TrailRenderer>().enabled = false;
    122.                         sphere.GetComponent<MeshRenderer>().enabled = false;
    123.  
    124.                     }
    125.                 }
    126.  
    127.  
    128.  
    129.             }
    130.  
    131.  
    132.  
    133.  
    134.         }
    135.  
    136.         if (chargeState == 3f)
    137.         {
    138.             if (XButtonUp == true)
    139.             {
    140.                 chargeState = 0f;
    141.             }
    142.         }
    143.  
    144.  
    145.     }
    146.  
    I realize that the bool is true and then false ALSO during the Update, so FixedUpdate registers it exactly the same...

    Any suggestions? I'm really new to programming, so explain as much as you can! Thanks in advance.
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,383
    You don't "transfer" data between Update and FixedUpdate. It basically looks like you're already doing what you're asking how to do, so I don't really understand your question.

    In the class scope you have some boolean's and you're populating them in Update. Later, Unity call's FixedUpdate and it reads those same boolean values. That's really all there is to it. That all happens in the same frame.
     
  3. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Update and Fixed Update are a little out of sync. What I would try is to not make the button down bool false in update, but only in fixed update. So you are essentially turning them on in update, and off in fixed update in case there is a delay and fixed update misses it. Whether that would have a different feel or not is hard to say.
     
  4. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,383
    Oh I see. Yeah it would only be valid on the frames where it was pressed and FixedUpdate was also called.

    Is there a reason you're even using FixedUpdate?
     
  5. GuyStreamsStuff

    GuyStreamsStuff

    Joined:
    Dec 22, 2017
    Posts:
    27
    How could I make it off in FixedUpdate? At the very end of it?


    It was my understanding that all physics stuff should be in FixedUpdate. Like I'm using AddForce and stuff like that.
     
  6. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,383
    Code (csharp):
    1.  
    2.         bool AButtonState;
    3.         bool AButtonWasReleased;
    4.         bool XButtonState;
    5.         bool XButtonWasReleased;
    6.  
    7.         private void Updates()
    8.         {
    9.             AButtonState = Input.GetButton("Fire1");
    10.             XButtonState = Input.GetButton("xBut");
    11.  
    12.             if (Input.GetButtonUp("Fire1"))
    13.             {
    14.                 AButtonWasReleased = true;
    15.             }
    16.             if (Input.GetButtonUp("xBut"))
    17.             {
    18.                 XButtonWasReleased = true;
    19.             }
    20.         }
    21.  
    22.         private void FixedUpdates()
    23.         {
    24.             [....]
    25.  
    26.             if (AButtonState)
    27.             {
    28.                 character.AddForce(transform.forward * homingSpeed * Time.deltaTime, ForceMode.Impulse);
    29.                 character.AddForce(transform.up * homingSpeed * 0.5f * Time.deltaTime, ForceMode.Impulse);
    30.  
    31.                 hasHomingAttacked = true;
    32.                 animator.SetBool("isHoming", true);
    33.                 chargeState = 0f;
    34.             }
    35.  
    36.             [....]
    37.  
    38.             if (XButtonWasReleased)
    39.             {
    40.                 XButtonWasReleased = false;
    41.                 chargeState = 2f;
    42.                 animator.SetBool("isCharging", false);
    43.                 animator.SetBool("IsRolling", true);
    44.             }
    45.         }
    So doing that will just store the button state, then have a bool that works as a flag when a button gets released in an update and you can just turn it off when you consume it.

    It seems like the release triggers don't really need to be in FixedUpdate since they're not doing anything with physics so you could probably move that logic out and start better organizing what absolutely must be in FixedUpdate to avoid confusion, maybe.
     
  7. GuyStreamsStuff

    GuyStreamsStuff

    Joined:
    Dec 22, 2017
    Posts:
    27
    Thanks for the help, I'll try right away and post the results here!!