Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by biebuster · Oct 02, 2015 at 02:11 PM · jumpdelaybasic programming

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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
             }
         }
 
 
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image biebuster · Oct 02, 2015 at 06:23 PM 0
Share

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.

avatar image PsyDev · Oct 02, 2015 at 06:39 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges