- Home /
Continuous Jumping using gravity
I can't understand why this code changes the player's speed when standing on a platform (so the speed should be 0). As a result, gravity also changes:
This is velocity.y:
The problematic function is this (if I remove it, velocity.y becomes 0 when the player is on a floor):
private void setJumpGravity()
{
if(playerRb.velocity.y < 0)
{
playerRb.gravityScale = fallJumpGravity;
}
else if(playerRb.velocity.y > 0 && Input.GetKey(KeyCode.Space) == false)
{
playerRb.gravityScale = spaceJumpGravity;
}
else
{
playerRb.gravityScale = defaultGravity;
}
}
The whole class is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private float horizontalInput;
[SerializeField] private float horizontalSpeed;
private Rigidbody2D playerRb;
private bool isGetSpaceDown;
[SerializeField] private float jumpForce;
[SerializeField] private float fallJumpGravity;
[SerializeField] private float spaceJumpGravity;
private float defaultGravity;
// Start is called before the first frame update
void Awake()
{
playerRb = gameObject.GetComponent<Rigidbody2D>();
defaultGravity = playerRb.gravityScale;
}
void Update()
{
horizontalInput = Input.GetAxisRaw("Horizontal");
if(Input.GetKeyDown(KeyCode.Space))
{
isGetSpaceDown = true;
}
}
// Update is called once per frame
void FixedUpdate()
{
playerRb.velocity = new Vector2(horizontalSpeed * horizontalInput, playerRb.velocity.y);
if(isGetSpaceDown == true)
{
playerRb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
isGetSpaceDown = false;
}
setJumpGravity();
//Debug.Log(playerRb.velocity.y);
Debug.Log(playerRb.gravityScale);
}
private void setJumpGravity()
{
if(playerRb.velocity.y < 0)
{
playerRb.gravityScale = fallJumpGravity;
}
else if(playerRb.velocity.y > 0 && Input.GetKey(KeyCode.Space) == false)
{
playerRb.gravityScale = spaceJumpGravity;
}
else
{
playerRb.gravityScale = defaultGravity;
}
}
}
Answer by HellsHand · Apr 08, 2021 at 01:19 PM
Easy fix is to add a bool for whether or not the player is jumping. Add a private bool isJumping = false
to your initializers.
if (isGetSpaceDown == true)
{
playerRb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
isJumping = true; //after adding upward force set isJumping to true
isGetSpaceDown = false;
}
Then change the section of code where you find the issue to:
private void setJumpGravity()
{
if (isJumping) //this will ensure gravity is only changed when in the air
{
if (playerRb.velocity.y < 0)
{
playerRb.gravityScale = fallJumpGravity;
}
else if (playerRb.velocity.y > 0 && Input.GetKey(KeyCode.Space) == false)
{
playerRb.gravityScale = spaceJumpGravity;
}
}
else
{
playerRb.gravityScale = defaultGravity; //now gravity will remain at it's default when not in the air
}
if(playerRb.velocity.y == 0) //set isJumping back to false when the player hits the ground
{
isJumping = false;
}
}
Thanks for the solution. I publish below another solution that I have found, maybe it can be useful to someone.
private void setJumpGravity()
{
if(playerRb.velocity.y < 0)
{
playerRb.gravityScale = fallJumpGravity;
}
else if(playerRb.velocity.y > 0 && Input.GetKey(KeyCode.Space) == false)
{
playerRb.gravityScale = spaceJumpGravity;
}
if(isCollidingGround() == true)
{
playerRb.gravityScale = defaultGravity;
}
}
where:
private bool isCollidingGround()
{
BoxCollider2D playerCollider = gameObject.GetComponent<BoxCollider2D>();
RaycastHit2D groundCastHit = Physics2D.BoxCast(playerCollider.bounds.center, playerCollider.bounds.size,
0, Vector2.down, 0.1f, JumpableLayer);
if(groundCastHit.collider == null)
return false;
return true;
}
Answer by singhlaxman9761 · Apr 08, 2021 at 12:35 PM
Ignore everything and use Physics material for jumping like bouncing ball :)