Skip to content

Instantly share code, notes, and snippets.

@saturngamesss
Created April 21, 2020 14:59
Show Gist options
  • Save saturngamesss/49f5f27a1eb34d3da6f1ddd88e86434c to your computer and use it in GitHub Desktop.
Save saturngamesss/49f5f27a1eb34d3da6f1ddd88e86434c to your computer and use it in GitHub Desktop.
Simple double jump code for Unity Engine
//FROM PROJECT ROROPE || REAL GAMES STUDIO
//************************************************
//realgamesss.weebly.com
//gamejolt.com/@Real_Game
//realgamesss.newgrounds.com/
//real-games.itch.io/
//youtube.com/channel/UC_Adg-mo-IPg6uLacuQCZCQ
//************************************************
using UnityEngine;
public class DoubleJump : MonoBehaviour
{
Rigidbody2D rb;
public float speed = 50;
public float jumpForce = 20;
public bool isGrounded;
public bool isJumping;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
if (Input.GetKey(KeyCode.D))
{
rb.velocity = new Vector2(speed, rb.velocity.y);
}
else if (Input.GetKey(KeyCode.A))
{
rb.velocity = new Vector2(-speed, rb.velocity.y);
}
else
{
rb.velocity = new Vector2(0, rb.velocity.y);
}
if (Input.GetKeyDown(KeyCode.Space) && isGrounded == true)
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
isJumping = true;
}
else if (Input.GetKeyDown(KeyCode.Space) && isJumping == true)
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
isJumping = false;
}
}
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
isGrounded = true;
}
}
void OnCollisionExit2D(Collision2D collision)
{
isGrounded = false;
}
}
@SrHoddot
Copy link

SrHoddot commented Jul 6, 2021

It's worked, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment