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");
}
}
}
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 :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613034657im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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