Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 stingman · Mar 15, 2012 at 02:58 AM · velocityjumpcharacter controllerflightfly

how can I make a character fly when holding down the jump button?

I have a character controlled with a character controller object. When I press the jump button my character jumps. If I press the button again in the air he jumps again (double jump). If I hold the jump button down I want the character to slowly fly upwards at a constant velocity, while still being able to move the character left and right ( It's a 2D platformer). When I release the jump button I want the character to fall down. I want their to be a certain time limit that the character can "fly" though. For instance I have game objects in the game that the character needs to collect to keep flying. So without collecting them he can fly for a maximum of 8 seconds. I have tried a lot of different approaches to this but cannot get this to work. I'm hoping someone might have some suggestions for me. Thank you!

Comment
Add comment · Show 2
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 IndieScapeGames · Mar 15, 2012 at 03:10 AM 0
Share

Please share some code that you do have. The more you share, the easier it would be to change it to what you're wanting.

avatar image syclamoth · Mar 15, 2012 at 03:16 AM 0
Share

@Chad$$anonymous$$- FYI, if you post comments as comments ins$$anonymous$$d of answers, you won't have to wait for the moderation queue...

1 Reply

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

Answer by syclamoth · Mar 15, 2012 at 03:10 AM

Well, I'm not too hot on specifics of CharacterControllers (Can't stand 'em, never use 'em), but I can outline some pseudocode for you. What I'm writing here assumes that you want the action to be something like-

tap jump once: jump

tap jump again: double-jump

hold jump having let go of jump after the double-jump: fly upwards, constant speed.

 bool hasDoubleJumped;
 public float maxFlyTime;
 float currentFlyTime;
 if(on ground)
 {
     hasDoubleJumped = false;
     currentFlyTime = 0;
 }
 if(jumpButton pressed down)
 {
     if(on ground)
     {
         jump;
     }
     if(inAir)
     {
         if(!hasDoubleJumped)
         {
             doubleJump;
             hasDoubleJumped = true;
         }
     }
 }
 if(jumpButton held down && hasDoubleJumped && currentFlyTime < maxFlyTime)
 {
     fly upward;
     currentFlyTime += Time.deltaTime;
 } else {
     apply gravity;
 }
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 stingman · Mar 15, 2012 at 03:38 AM 1
Share

Thank you Syclamoth. Your code sample helped me out a bit. I'm still tweaking it but I'm on the right track. I'll post the code once I get it all working.

avatar image stingman · Mar 15, 2012 at 04:30 AM 0
Share

no reason to post my code... just do exactly what syclamoth said... got it working. also... when character hits the ground make sure you reset the currentFlyTime to 0.0 otherwise you cannot fly again :)

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

creating a jump pad for a character controller 2 Answers

Third person controller in Flight 1 Answer

Make NavMesh Agent Jump on Off-Mesh Links 0 Answers

Why won't this jump code work? 1 Answer

Jumping not always work 2 Answers


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