- Home /
need to create delay, for something that will execute every frame, for a bit of time
So let's say the player jumps. Now, 16 frames after he pushed the button, i wan't some custom physics that smooth the top of the jump, so he stays at the max height longer. These custom physics would require me to execute some code every frame. and then when i was done with that, they shoulnd't activate any more. Now, InvokeRepeating() doesn't let you set delay for when to invoke, and a method will only activate once with Invoke() Right now i'm making a fake delay by, activating a bool when the delay needs to start. This bool will then include some code that will activate every frame, and add to an int. And when that int has reached, say, 20 it will activate ANOTHER bool, that will then execute the code. Not great code at all. Thanks for the help!
Answer by PsyDev · Oct 02, 2015 at 03:46 PM
Sounds like you are sort of on the right track. You could keep it clean by creating a small state machine for the jump, and make delays time dependent rather than frame-rate dependent.
enum JumpState
{
IDLE,
RISING,
TOP,
FALLING
}
JumpState jumpState = JumpState.IDLE;
float jumpTime;
/////
switch (jumpState)
{
case JumpState.IDLE:
if (doJump)
{
jumpTime = 0;
jumpState = JumpState.RISING;
// Start your physics jump here
}
break;
case JumpState.RISING:
jumpTime += Time.deltaTime;
// This could be base on height or time
if (jumpTime > jumpRiseTime)
{
jumpTime = 0;
jumpState = JumpState.TOP;
// Switch to your custom jump-float movment
}
break;
case JumpState.TOP:
// Doing your jump-float stuff here
if (jumpTime > jumpTopTime)
{
jumpTime = 0;
jumpState = JumpState.FALLING;
// Switch back to physics fall
}
}
Thanks you so much! Seriously that solution is brilliant. Very glad you took the time to code it, because i'm pretty bad at enums. But looking at this it makes a lot of sense.
No problem. Whenever I start depending on a few bools to deter$$anonymous$$e state, I break it out into a switch-based state machine. Do a search on Stack Exchange about switch vs if-else and you can get a broad view of opinions and pros and cons.
ps If you think this answers your questions, please accept the answer or upvote. I just created this account and have almost no karma/reputation points :^(
Your answer
Follow this Question
Related Questions
How can I set a timer? 1 Answer
Coroutine doesnt work. Delayed Jump. 1 Answer
Can't Modify PlatformerCharacter2D Jump Force From Other Script 0 Answers
Jump problem (2D) 0 Answers
Delay Jump 1 Answer