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 /
  • Help Room /
avatar image
0
Question by ThomasAshton · Aug 15, 2021 at 09:58 PM · c#errorbugunity 2d

A key will not respond

In a platform game I am making, I programmed the player to jump assuming the up arrow is pressed and the bool "onGround" is equal to true. Sometimes it inexplicably works even when onGround equals true. So I had "Jump" printed to the console every time I pressed up arrow. When onGround equaled true, and I pressed the up arrow, occasionally it would, regardlessly, not work. Please help!

Code:

 public float speed;
 public float jumpForce;

 Rigidbody2D rb;
 public GroundCheck groundCheck;
 Animator anim;
 SpriteRenderer spriteRend;

 void Awake()
 {
     rb = GetComponent<Rigidbody2D>();
     anim = GetComponent<Animator>();
     spriteRend = GetComponent<SpriteRenderer>();
 }
 void FixedUpdate()
 {
     anim.SetFloat("Velocity", rb.velocity.y);
     anim.SetFloat("Speed", Mathf.Abs(rb.velocity.x));

     if (Input.GetKeyDown(KeyCode.UpArrow)&&groundCheck.onGround)
     {
         rb.velocity = new Vector2(0, 0);
         rb.AddForce(jumpForce * Vector2.up, ForceMode2D.Impulse);
         anim.SetBool("IsJumping", true);
         Debug.Log("jump");
     }
     if (Input.GetKey(KeyCode.RightArrow))
     {
         rb.velocity = new Vector2(+speed, rb.velocity.y);
         transform.localScale = new Vector3(1, 1, 1);
     }
     else if (Input.GetKey(KeyCode.LeftArrow))
     {
         rb.velocity = new Vector2(-speed, rb.velocity.y);
         transform.localScale = new Vector3(-1,1,1);
     }
     else
     {
         rb.velocity = new Vector2(0, rb.velocity.y);
     }
 }
 public void StopJumpAnim()
 {
     anim.SetBool("IsJumping", false);
 }
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

Answer by rage_co · Aug 16, 2021 at 04:25 AM

The problem is that, while continous inputs like checking if a key is being held down are totally fine in fixed update, One time Functions like checking if a key is pressed (GetKeyDown)..are better in Update as it can result in input loss in fixedupdate, it's totally healthy to Move your jump command to the update method along with all the physics is it, because it's a one time run so you don't need to worry about them adding up if the framerates are high

Comment
Add comment · Show 1 · 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 ThomasAshton · Aug 18, 2021 at 05:56 PM 0
Share

It works well! Thank you!

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

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

How do I make a ball bounce in a circle? 0 Answers

Menu object not responding 0 Answers

Level Start error :/ 1 Answer

Sound in Unity is not working! WHY? 0 Answers

Unity 2017 or 5.6 Doesn't work after install. Editor is broken. 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