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 NolanOc · Jan 23, 2018 at 11:13 PM · 2d game2d-platformerjumpjumping

Long press for charged Jump

Hey, question for you guys, I am trying to make a player charge up a jump by long pressing a button and then after a time or when released the jumpforce will be equal to how long he pressed the button for. I've seen some tutorials on how to make the player float the longer you press but I really wanted to make it charge up and then release instead. Any ideas?

public class StartScreen : MonoBehaviour {

 void Jump()
 {
     rig.velocity = new Vector2 (rig.velocity.x, jumpForce);
 }

}

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 LTonon · Jan 23, 2018 at 11:41 PM

Ok, I gave it a try and came up with this code, hope it does what you want:


 // Update will receive the input data
 void Update () {
         
         // If pressing Space, charge the variable charger using the Time it's being pressed.
         if (Input.GetKey(KeyCode.Space))
         {
             charger += Time.deltaTime;
         }
 
         // On release, set the boolean 'discharge' to true.
         if (Input.GetKeyUp(KeyCode.Space))
         {
             discharge = true;
         }
 }
 
 
 // Using fixed update since we are dealing with Unity's Physics
 private void FixedUpdate()
 {
     // On (discharge == true)
     if (discharge)
     {
         // Set jump force and give new velocity
         jumpForce = 10 * charger;
         rb.velocity = new Vector2(rb.velocity.x, jumpForce);
         
         // Reset discharge and charger values
         discharge = false;
         charger = 0f;
     }
 
 }
Comment
Add comment · Show 4 · 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 NolanOc · Jan 24, 2018 at 12:13 AM 0
Share

I appreciate you help! hmm it seems to be working okay, however the number is pretty unpredictable for some reason and I'm not sure why. for instance if I hold the button for a while it sometimes jumps the player shorter than I short press. Which is a pretty confusing...

Also the charged float seems to be capping off on its own even though I didn't tell it to do that. Any ideas?

avatar image LTonon NolanOc · Jan 24, 2018 at 12:25 AM 0
Share

It is unpredictable because I used Time.deltaTime and this number corresponds to the time in seconds it took to complete the last frame. It's unpredictable but it should not present that much difference, it's the most reliable way that I can think of.

What do mean "capping off"? Resetting to 0? Did you want to keep adding speed to the Game Object without stopping?...

avatar image NolanOc · Jan 24, 2018 at 12:26 AM 0
Share

O$$anonymous$$AY I made the mistake, I used Get$$anonymous$$ouseButtonDown ins$$anonymous$$d of Get$$anonymous$$ouseButton, then I just added an if charger > 1 , charger = 1.

You totally saved me! Thanks so much!

avatar image LTonon NolanOc · Jan 24, 2018 at 12:27 AM 0
Share

No problem! Glad to help!! ^^

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

85 People are following this question.

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

Related Questions

Raycast2D not working 1 Answer

Problem with Jump 1 Answer

Jump only when Jump button is pressed - not held 1 Answer

I need help with fixing my "up special" mechanic. 1 Answer

Problem between my player jump and player movement 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