- Home /
Question by
Resiri · Jun 13, 2021 at 12:01 PM ·
c#2d2d-platformerglide
2D Platformer Gliding
Hi there, I would like to make it so that my script allows you to be able to glide/hover when holding the space button when the player is not grounded
I was hoping for some help as I'm not sure what the best way of doing it is with the script I have
Here is the script: {
private Rigidbody2D rb;
public float jumpForce;
public bool isGrounded = false;
public Transform isGroundedChecker;
public float checkGroundRadius;
public LayerMask groundLayer;
public float fallMultiplier = 2.5f;
public float lowJumpMultiplier = 2f;
public float rememberGroundedFor;
float lastTimeGrounded;
public int defaultAdditionalJumps = 1;
int additionalJumps;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
Jump();
BetterJump();
CheckIfGrounded();
}
void Jump()
{
if (Input.GetKeyDown(KeyCode.Space) && (isGrounded || Time.time - lastTimeGrounded <= rememberGroundedFor || additionalJumps > 0))
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
additionalJumps--;
}
}
void CheckIfGrounded()
{
Collider2D colliders = Physics2D.OverlapCircle(isGroundedChecker.position, checkGroundRadius, groundLayer);
if (colliders != null)
{
isGrounded = true;
additionalJumps = defaultAdditionalJumps;
}
else
{
if (isGrounded)
{
lastTimeGrounded = Time.time;
}
isGrounded = false;
}
}
void BetterJump()
{
if (rb.velocity.y < 0)
{
rb.velocity += Vector2.up * Physics2D.gravity * (fallMultiplier - 1) * Time.deltaTime;
}
else if (rb.velocity.y > 0 && !Input.GetKey(KeyCode.Space))
{
rb.velocity += Vector2.up * Physics2D.gravity * (lowJumpMultiplier - 1) * Time.deltaTime;
}
}
}
Thank you for your time :)
Comment
You could add a force upwards of the player whilst holding space in the air. Just enough to slow their descent, and not too much to let them fly.
Your answer
![](https://koobas.hobune.stream/wayback/20220613034942im_/https://answers.unity.com/themes/thub/images/avi.jpg)