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 /
  • Help Room /
avatar image
0
Question by Matt500b · Oct 18, 2015 at 07:55 PM · movementjumpinggetkeydowngetkeygetkeyup

GetKeyDown breaks jumping vs GetKey

Hi all,

I am creating my own physics for a character in a 2D side-scroller and one of the mechanics I want to have is 'Short Hops'.

Whilst looking around everyone uses the idea of GetKeyDown and GetKeyUp and using timers etc. However using GetKeyDown rather than GetKey, the jumping doesn't work anymore. I think it might have something to do with my use of grounded/air states but I am unsure.

Using GetKeyDown the character jumps about 2 cm above the ground and falls rapidly, however using GetKey the character jumps as expected using the formula:

 public float timeToApex = 0.4f;
 public float jumpHeight = 4;
 
 gravity = -(2 * jumpHeight) / Mathf.Pow(timeToApex, 2);
 jumpVelocity = Mathf.Abs(gravity) * timeToApex;

Here is a simple version of my code:

  if (grounded) 
  {
      if (Input.GetKeyDown(KeyCode.Space))
      {           
          VelocityY = jumpVelocity;                    
      }
 }

Thanks in advance, Matt

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
2
Best Answer

Answer by dkjunior · Oct 19, 2015 at 05:13 AM

The difference between GetKeyDown and GetKey is that the GetKeyDown is triggered only once on the first frame the key was pressed while GetKey will be triggered on every frame while the key is pressed. So I'd guess the difference in observed behavior is due to the fact that even if you quickly press/release the space button GetKey version manages to get several VelocityY updates in, while GetKeyDown only sets it to jumpVelocity once and then lets gravity kill it.

Intuitively, GetKeyDown appears to be a better choice in your situation. You just need to tweak your jumpVelocity and gravity to make the jump appear as you want (it seems that you need to increase the jump velocity and decrease gravity).

Comment
Add comment · Show 5 · 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 Matt500b · Oct 19, 2015 at 06:00 PM 0
Share

Hi dkjunior, perhaps there is a problem within the script that is conflicting with this jumping method - in an old script I used this exact same mechanic and it worked; however I am rewriting the script to improve it. Would it be possible to run through the script? $$anonymous$$ainly split into two sections, a grounded state and an air state. http://pastebin.com/ze4zfrFk

avatar image dkjunior Matt500b · Oct 20, 2015 at 04:52 AM 0
Share

Glanced over the code briefly. What is the purpose of the Space $$anonymous$$eyUp section below?

    if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.Space))
     {
        if (VelocityY > $$anonymous$$JumpVelocity)
        {
           VelocityY = $$anonymous$$JumpVelocity;
        }
     }

This is something that may be causing the abrupt fall.

avatar image Matt500b dkjunior · Oct 20, 2015 at 01:28 PM 0
Share

The code is just an attempt at short hops, having tried multiple different methods that was just the last one I've tried. However commenting out the Get$$anonymous$$eyUp statement doesn't change anything when pressing space.

Still leaving it as Get$$anonymous$$ey rather than Get$$anonymous$$eyDown works perfectly and as such am very confused to why this is.

I will continue to debug the code until I find what part is conflicting to cause this however if you have any more suggestions that would be fantastic

Show more comments

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

32 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

Related Questions

Regularly rotation control with keys,Regularly Rotation control by keys 0 Answers

need help with in-air movement 0 Answers

need help with jumping script 1 Answer

Jump logic issues 0 Answers

Player looses ability to jump further right player moves. 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