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 CJWilmot · Jun 07, 2021 at 10:27 AM · jumpjumping

Unity 2D - Jumping Isn't Smooth

Hi Everyone,

I'm new to Unity 2D, so please bear with me.

I've created a basic 2D game with UI buttons to move the player left and right as well as being able to jump. All of the buttons work but the jump isn't smooth. When holding the left of right UI button and then pressing jump the player slowly floats upwards.

The controller is below. Any help is greatly appreciated!

 private void Start()
 {
     rb2d = GetComponent<Rigidbody2D>();
     animator = GetComponent<Animator>();
     spriteRenderer = GetComponent<SpriteRenderer>();

     moveLeft = false;
     moveRight = false;
     isGrounded = false;
     
 }

 //FUNCTION - Left Button Pressed
 public void PointerDownLeft()
 {
     moveLeft = true;
     Debug.Log("Function - PointerDownLeft");
 }

 //FUNCTION - Left Button Pressed
 public void PointerUpLeft()
 {
     moveLeft = false;
     Debug.Log("Function - PointerUpLeft");
 }

 //FUNCTION - Right Button Pressed
 public void PointerDownRight()
 {
     moveRight = true;
     Debug.Log("Function - PointerDownRight");
 }

 //FUNCTION - Right Button Pressed
 public void PointerUpRight()
 {
     moveRight = false;
     Debug.Log("Function - PointerUpRight");
 }

 //FUNCTION - Jump Button Pressed
 public void PointerDownJump()
 {
     moveJump = true;
     
 }

 //FUNCTION - Jump Button Pressed
 public void PointerUpJump()
 {
     moveJump = false;

 }

 private void GroundCheck()
 {
     isGrounded = false;

     Collider2D[] colliders = Physics2D.OverlapCircleAll(groundCheckCollider.position, 0.2f, groundLayer);
     if (colliders.Length > 0)
     {
         isGrounded = true;
         Debug.Log("Player touching ground");
     }
     else
     {
         Debug.Log("Player is not touching ground");
     }
 }


 public void PlayerMovement()
 {
     if (moveLeft)
     {
         horizontalMove = -playerSpeed;
         spriteRenderer.flipX = true;
     }
     else if (moveRight)
     {
         horizontalMove = playerSpeed;
         spriteRenderer.flipX = false;
     }
     else
     {
         horizontalMove = 0;
     }

 }

 private void Update()
 {
     PlayerMovement();
 }

 private void FixedUpdate()
 {

     GroundCheck();

     rb2d.velocity = new Vector2(horizontalMove, rb2d.velocity.y);

     if (moveJump == true && isGrounded)
     {
         rb2d.velocity = Vector2.up * jumpSpeed;
     }

     if (moveLeft)
     {
         animator.Play("PlayerRun");
     }
     else if (moveRight)
     {
         animator.Play("PlayerRun");
     }
     else if (isGrounded == false)
     {
         animator.Play("PlayerJump");
     }
     else
     {
         animator.Play("PlayerIdle");
     }

 }

}

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 Kaemcosi · Jun 08, 2021 at 04:51 PM

In your FixedUpdate() method you set your rigibody's velocity to a new Vector2 with your horizontalMovement on the x and the vertical velocity on the y. This seems fine. However, when you jump you overwrite this velocity with a new Vector2 that only has a non-zero value for the Y, so your horizontal movement will stop and you will only move up.

I think in your case it's better to use rb2d.AddForce() to make your object jump, instead of setting the velocity. https://docs.unity3d.com/ScriptReference/Rigidbody2D.AddForce.html

Let me know if it worked :)

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

163 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

Related Questions

Character jumping too fast 1 Answer

Jumping higher the more you hold space! 1 Answer

Moving and jumping 1 Answer

Need help with jumping 0 Answers

Fix for inconsistent jump height? 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