Search Unity

Calling Method in FixedUpdate vs If Statement

Discussion in 'Scripting' started by totallynotlace, May 11, 2018.

?

FixedUpdate vs If

  1. Put your method in the If Statement

  2. Leave it in the update method.

  3. Your over complicating this, it doesn't matter.

Results are only viewable after voting.
  1. totallynotlace

    totallynotlace

    Joined:
    Oct 19, 2017
    Posts:
    4
    I'm wondering what would be more efficient. I have method that executes on a keystroke, and I dont know if i should declare it in an if statement in the update method, or call my method in the update method, with the check for the keystroke in the method itself.

    Code (csharp):
    1.  
    2.     void FixedUpdate () {
    3.             PlayerisAttacking();
    4.     }
    5.  
    or
    Code (csharp):
    1.  
    2.     void FixedUpdate () {
    3.         if (Input.GetKeyDown (pAttackKey)&& pCanAttack)
    4.         {PlayerisAttacking();}
    5.     }
    6.  
     
  2. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,190
  3. totallynotlace

    totallynotlace

    Joined:
    Oct 19, 2017
    Posts:
    4
    oh. woops. Ill fix that.. lol


    I can fix my code, but not the poll. Alas I hath failed this thread.... You can shoot me with arrows now...


    Still though, Update or If statement? I've been just calling the methods and putting the if check into the method for the keystroke.
     
    Last edited: May 11, 2018
  4. UziMonkey

    UziMonkey

    Joined:
    Nov 7, 2012
    Posts:
    206
    Even with the confusion about FixedUpdate cleared up, I'm confused about what you're really asking here. If you're trying to see if the attack button is pressed and the pCanAttack flag is true, then attack then... well, there no real other way to do that. Don't be too concerned about the efficiency of these things right now, just be concerned with getting them working. Later on you can address it if it's inefficient.

    Also, the issue with FixedUpdate is that it can be called any number of times per frame, usually 0,1 or 2. It does this to keep the physics engine running at a constant pace even if your frame rate is not constant. If you put things like this in FixedUpdate, they may run 0, 1 or 2 times per frame and if you're using Input.GetKeyDown it can miss input if you happen to hit the key on a frame where FixedUpdate runs 0 times. It can also run twice if your input code is not done correctly. It's best to stick to Update for anything but physics.
     
  5. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    In most case scenarios you would want to use Update for polling inputs, otherwise results might not be consistent.
    As for the method call - if your attack does something with physics, use FixedUpdate() and check for the flag received from an Update.

    But then again, it all about the context, what you're trying to achieve. If it's some kind of fire and forget event, then you might want to use a single event inside .Update() input check.
     
  6. totallynotlace

    totallynotlace

    Joined:
    Oct 19, 2017
    Posts:
    4
    I will probably have it do some sort of knock back effect later. I will have that run in fixedupdate. I changed the script already to run in the Update method rather.

    I think a better way of wording this would be, I'm going to be calling a bunch of methods in update, would it be better to poll for the keystroke in update, or call the method in update and poll for the keystroke in the method.

    I'm going to roll with what VirgilUa suggested and put the if statement that checks for the keystroke inside of the update method instead of the method that I'm calling.
     
  7. Deleted User

    Deleted User

    Guest

    but when you do that way it becomes little messy I think. I am junior and I am not sure which one is better for tidying code