Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Jfred94 · Nov 19, 2021 at 06:41 PM · unity 2djumpphysics2djumpingjumping object

my jump key works about once every 10 presses

Basically I have a problem, which is that I have a presumably working jump script, but when I try to jump it has a 10% chance to actually jump (around 10%, it's very random, sometimes it works twice in a row)


I don't want it to be forced to only jump when it's on the ground yet, I just want it to jump consistently.

My unity scene looks like this:

alt text

the green square is my player:

  • rigidbody, frozen Z rotation

  • box collider, nothing changed

  • script "PlayerScript" with a public Rigidbody2D variable, containing the Player gameobject's rigidbody2D via the unity inspector.


the white rectangle is the ground:

  • box collider, nothing changed


here's the PlayerScript script (which is essentially just a jump):

 using UnityEngine;
 
 public class PlayerScript : MonoBehaviour
 {
     public Rigidbody2D rb;
 
     void FixedUpdate()
     {
         if (Input.GetKeyDown(KeyCode.Space))
         {
             rb.velocity = Vector2.up * 10f;
         }
     }
 }
 

This is all I've done after creating a new unity 2D project. And yet, the jump key does't work about 90% of the time.


My guess is that it has to do with how my unity is set up at the moment, and that there's nothing wrong with the code. Thank you for taking the time to help me, I appreciate you very much! :)

EDIT: after changing FixedUpdate() to Update() it seems to work correctly, although I'm not sure if it's a good idea in the long term. Feedback is still greatly appreciated, even though the problem I had is technically fixed for now.

scene.png (2.4 kB)
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 gipsnichmeer · Nov 19, 2021 at 07:21 PM

Yes, you should collect user input in Update() since Unity takes input from the operating system every frame. However frames don't always have the same time between them. That's why you should use FixedUpdate() to move things (or generally do things that should constantly have the same speed) A good solution would be to get your input in Update() and then perform the action in FixedUpdate() like this:

  using UnityEngine;
  
  public class PlayerScript : MonoBehaviour
  {
      public Rigidbody2D rb;
      private bool isSpacePressed = false;
  
      void Update()
      {
          if (Input.GetKeyDown(KeyCode.Space))
          {
                isSpacePressed = true;
          }
      }
      void FixedUpdate()
      {
           if(isSpacePressed)
               rb.velocity = Vector2.up * 10f;
      }
  }
Comment
Add comment · Show 3 · 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 Jfred94 · Nov 21, 2021 at 10:37 AM 0
Share

It all makes sense now! Thank you so much for the answer! :)

avatar image gipsnichmeer Jfred94 · Nov 21, 2021 at 04:11 PM 0
Share

Happy to help. By the way, you could also do everything inside of Update() but then multiply your velocity with Time.deltaTime (the time in seconds that passed since the last frame)

avatar image rh_galaxy · Nov 21, 2021 at 10:58 AM 0
Share

There are usages for taking the Input in FixedUpdate as well, if you do it right. For example if you want to replay your moves.

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

143 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 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

Im trying to make it so it limits jumps in my 2d game. But when I play it, It only jumps once then your not able to jump again. Anyhelp? I need a answer fast because this is for the blackthornprod game jam 2 Answers

How can I limit jump height in unity? 3 Answers

2d rigidbody jump not working 1 Answer

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

Unity physics going against a wall and trying to jump fails while still running at it 0 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