Calling Method in FixedUpdate vs If Statement

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.

    void FixedUpdate () {
            PlayerisAttacking();
    }

or

    void FixedUpdate () {
        if (Input.GetKeyDown (pAttackKey)&& pCanAttack)
        {PlayerisAttacking();}
    }

I’m more concerned with the reason you have the code in FixedUpdate. FixedUpdate is only intended for physics and trying to use it in a way other than that can very easily lead to problems.

https://discussions.unity.com/t/530594

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.

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.

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.

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.

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