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 /
avatar image
0
Question by danilomacb · Jul 20, 2021 at 08:41 PM · physicsjumpaxishorizontalwalk

The player doesn't jump to the left with the W key pressed

For some reason, when I try to jump to the left with W it doesn't work. I implemented the jump and the walk, in 2 separated functions on the player controller, and works fine when I don't press W. And also the jump to the left works fine with any other key pressed.

What happens is if the player was pressing W, he can jump or walk to the left, but not both at the same time. If the player presses W and walks to the left after, the jump doesn't work. If the player presses W and jumps, he can't go to the left in the air. Ironically it works if the player presses W and jumps to the right.

My first thought was the vertical axis because I am using the horizontal axis, but I try to jump to the left with the UP arrow key pressed, and works. The jump to the left also works fine with the S key pressed.

I don't know what is the reason for that, but I don't think this is a problem for my code. I suspected it was a problem with the Unity editor, so I created a build to test, and the result was the same.

The player gameobject has a transform, a dynamic rigidbody2d with 5 of gravity scale, collision detection continuous, freeze rotation on z, a simple box collider 2d, a sprite renderer, and the player controller script.

This is the PlayerController script:
(I set speed 10f and jump 20f in the inspector)

 using UnityEngine;
 
 public class PlayerController : MonoBehaviour
 {
     [SerializeField] private float speed;
     [SerializeField] private float jump;
 
     private bool canJump = true;
 
     private Rigidbody2D playerRigidbody2D;
 
     private void Start()
     {
         playerRigidbody2D = GetComponent<Rigidbody2D>();
     }
 
     private void Update()
     {
         Walk();
         Jump();
     }
 
     private void Walk()
     {
         float horizontalMovement = Input.GetAxis("Horizontal");
 
         if(horizontalMovement > 0 || horizontalMovement < 0)
         {
             playerRigidbody2D.velocity = new Vector2(horizontalMovement * speed, playerRigidbody2D.velocity.y);
         }
     }
 
     private void Jump()
     {
         if (Input.GetKeyDown(KeyCode.Space) && canJump)
         {
             canJump = false;
 
             playerRigidbody2D.velocity = new Vector2(playerRigidbody2D.velocity.x, jump);
         }
     }
 
     private void OnCollisionEnter2D(Collision2D collision)
     {
         if(collision.gameObject.tag == "Ground")
         {
             canJump = true;
         }
     }
 }



I created a simple project to reproduce this bug, and I put it in Github in the following link: https://github.com/danilomacb/JumpToLeftWithWPressedBug

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

0 Replies

· Add your reply
  • Sort: 

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

200 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

Related Questions

2D Plane Axis Restriction, Photon Networking & Physics... 0 Answers

Velocity and Animator change while game is Paused 1 Answer

Sphere get stuck in floor and fails to jump? 1 Answer

Make a ball jump on plane collision 1 Answer

Equivalent to this using torque 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