- Home /
Function being called twice when it shouldn't be
Alright so I've got a jump function and for some reason it can be called more then once (causing superjumps). This happens more often on hills. Problem is my code shouldn't even let that happen so I have no idea how to fix it.
In function FixedUpdate (I'm gonna change it to update when I clean up my code) I have this:
if (!Stunned && Grounded && Input.GetButtonDown("Jump") && !IsJumping)
{
Jump ();
IsJumping = true;
}
Pretty self explanatory. The problem in my jump function is from these lines of code:
if (!Grounded)
{
LeftGround = true;
}
if (Grounded && LeftGround)
{
IsJumping = false;
//other code stopping the Jump function that's not important is here
}
That section of code is in a for loop using yield new WaitForFixedUpdate() at the end. I wanted the function to cancel when they land and doing it this way prevented the function from cancelling instantly.
So, any ideas as to what is causing the problem? I just can't see how it can be called twice due to IsJump so I'm clueless.
Answer by syclamoth · Sep 17, 2013 at 06:28 AM
Input.Whatever does not get reset until the next 'Update' call! This means that if you are experiencing low frame rates, you can have two calls to 'FixedUpdate' that both receive the same buttonpress. As you suspected, moving this stuff to 'Update' is itself a fix.
in short: Never process one-time events in FixedUpdate. You might miss it or process it multiple times.
Also, be very careful when designing systems that must interact between the Update loop and the FixedUpdate loop! You can never be sure which of those loops will be updating more often, and if your systems are too closely coupled you can end up invalidating the state of one or the other.
Your answer
Follow this Question
Related Questions
No proper Update and function call between objects ? 1 Answer
first function dismiss 0 Answers
Shooting and Flash 2 Answers
How to find weather the function execution completed or not 2 Answers