Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 coolalan2016 · Mar 10, 2020 at 09:18 AM · movement scriptmovements

[2d] Player sometimes doesn't jump up while moving and jumping at the same time

My issue is that the player sometimes (occasionally) doesn't jump up while moving. It sometimes works, sometimes don't. It's weird, here's the code:

 [SerializeField] private LayerMask platformsLayerMask;

 public Rigidbody2D rb;

 public float MovementSpeed = 100f;

 public float JumpingHeight = 100f;

 public BoxCollider2D bc;

 public float fallMultiplier = 2.5f;
 public float lowJumpMultiplier = 2f;

 void Awake()
 {
     rb = transform.GetComponent<Rigidbody2D>();
     bc = transform.GetComponent<BoxCollider2D>();
 }

 void FixedUpdate()
 {
     if (IsGrounded() && Input.GetKeyDown(KeyCode.Space))
     {
         rb.velocity = new Vector2(rb.velocity.x, JumpingHeight * Time.deltaTime);
         Debug.Log("Jumping");
     }
     HandleMovement();

 }

 private bool IsGrounded()
 {
     RaycastHit2D raycastHit2d = Physics2D.BoxCast(bc.bounds.center, bc.bounds.size, 0f, Vector2.down, 1f, platformsLayerMask);
     Debug.Log(raycastHit2d.collider);
     return raycastHit2d.collider != null;
 }

 void HandleMovement()
 {
     if (Input.GetKey(KeyCode.A))
     {
         rb.velocity = new Vector2(-MovementSpeed * Time.deltaTime, rb.velocity.y);
         Debug.Log("Going Left");
     }
     else
     {
         if (Input.GetKey(KeyCode.D))
         {
             rb.velocity = new Vector2(+MovementSpeed * Time.deltaTime, rb.velocity.y);
             Debug.Log("Going Right");
         }
         else
         {
             //no keys pressed
             rb.velocity = new Vector2(0, rb.velocity.y);
             Debug.Log("No key pressed");
         }
     }
     if (rb.velocity.y < 0) //reponsive jumping and falling
     {
         rb.velocity += Vector2.up * Physics2D.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
     }
     else if (rb.velocity.y > 0 && Input.GetKeyDown(KeyCode.Space))
     {
         rb.velocity += Vector2.up * Physics2D.gravity.y * (lowJumpMultiplier - 1) * Time.deltaTime;
     }

 }

}

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 Roger_0123 · Mar 10, 2020 at 03:27 PM

Hello, the problem is because you try to get spacebar down in FixedUpdate. However, FixedUpdate may not run in all frames like Update, so what happens is that sometimes you press the Space button when FixedUpdate has not been called yet, and so it reverts to false in the next frame. What you can do is assigning it to a boolean in Update like this:

 void Update(){
    if(Input.GetKeyDown(KeyCode.Space){
      hasPressedJump = true;
    }        
 }
 
 void FixedUpdate(){
    if(IsGrounded() && hasPressedJump){
      //your code
      hasPressedJump = false;
    }
 }
Comment
Add comment · 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

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

126 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

Related Questions

Snake Movement Problem 0 Answers

HOW TO MAKE A NPC GO FORWAR 0 Answers

What should i write that when i press "Shift" it will speed up idk how shift is named? Here's the code: 1 Answer

MoveTowards is moveing my object to random position when i click it is already on a way 1 Answer

How to make fish movement with keyboard arrows look natural? 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